Click on element via Javascript console - javascript

Imagine there is an element #button1 on website which is watched by jQuery:
$('#button1').click(function(){
console.log("you clicked");
};
So how do I click this #button1 element via JavaScript console? Is there a command like click("#button1")? Thanks.

You can trigger click functio like bellow
Using JQuery
$('#button1').click()
using simple JS
document.getElementById('button1').click();

You can trigger a click by omitting the callback function:
$('#button1').click();

You can not click, but simulate or trigger click. Please refer Creating and triggering events

You can achieve this without jQuery:
document.getElementById("button1").click(); // clicks the button

You can also use this apart from the answers mentioned already:
$( "#button1" ).trigger( "click" );

Related

Simulating button click in logon page

I'm trying to use jquery to trigger (click) a button on a logon page. Standard javascript works:
document.getElementById('loginButton').click();
However, when using jquery like this nothing happens.
$("#loginButton").click();
I've tested that jquery is working and can find the loginButton using this.
console.log($("#loginButton").length) which returns a 1.
$("loginbutton").trigger("click");
You need to call that click function:
$( "#target" ).click();
OK So this was what I needed to do for jquery to work.
$("#loginButton")[0].click();
Strange because document.getElementById('loginButton').click() worked fine as it was. Can anyone explain why this might be ?

Triggering button click in jQuery not working

Using dot.js I'm adding a button to a specific web page that, when clicked, should add some text to a text field and then trigger another button to also be clicked. I simulate this by adding a click handler to my button which has this code:
var button = $('.some-class').find('button')[0];
console.log(button); // element I expect
button.click();
However, this doesn't work and I'm not sure why. If instead of .click() I perform .remove(), the button is removed from the page. If I use the console to execute the same code, the button does get clicked. This tells me I do have the right element, but there is something wrong with the click() event specifically.
Can someone explain why this isn't working in either Safari or Chrome? I've tried a lot of different things, but I'm new to jQuery so I'm probably missing some detail in how that works.
We went to the bottom of this in the chat. What probably caused the problem was another event-handler attached to (possibly) body, that undid the click.
So the solution was to stop the event from propagating:
event.stopPropagation();
While assigning the click event handler to the button you should use jquery on
This should ensure that whenever a new button with added with same selector (as in when event was assigned), event handled will be assigned to that button
Some examples here
The problem is the click() function is from jquery and you're attempting to fire the click function from the DOM object.
Try
$(button).click();
Here's a plunk.
http://plnkr.co/edit/2pcgVt
You can use the following statement.
var button = $('.some-class').find('button')[0].trigger('click');
try jquery's trigger() function:
$(button).trigger('click');
see jsfiddle: https://jsfiddle.net/665hjqwk/

Why can't I trigger the default href on another anchor with JS?

I'm posting this because I can't find the same question elsewhere.
I'm trying to trigger the default action of an anchor but calling .click() or .trigger('click') in the click handler of another anchor.
Like so:
HTML:
<!-- I want to simulate a user clicking link2 when the user actually clicks link 1. -->
<!-- My guess is that triggering click just triggers any JS-bound click handlers. But that would defeat the point of e.preventDefault() when you usually have to call this to stop the default href being followed -->
<a id="link1" href="#">Click</a>
<a id="link2" target="_blank" href="http://google.com">Link 2</a>
JS:
$(document).ready(function(){
$('#link1').on('click', function(){
$('#link2').click();
$('#link2').trigger('click'); // Neither work
});
});
I feel like such a noob but nothing happens on click. Is this something that is blocked for security or accessibility?
I do not want to use window.open();
fiddle: http://jsfiddle.net/0hggdkzb/
try
jQuery(document).ready(function(){
$('#link1').on('click', function(){
// $('#link2').click().css('color','red');
document.getElementById("link2").click();
});
});
DEMO
Or
you can trigger event $('#link2')[0].click();
Triggering-event-handlers clears about this,
The .trigger() function cannot be used to mimic native browser events,
such as clicking on a file input box or an anchor tag. This is
because, there is no event handler attached using jQuery's event
system that corresponds to these events.
The jQuery UI Team created jquery.simulate.js in order to simplify
triggering a native browser event for use in their automated testing.
Its usage is modeled after jQuery's trigger.
$( "a" ).simulate( "click" );
And for your problem you have to use javascript core event as #Bala answered like,
$(function(){
$('#link1').click(function(){
$('#link2')[0].click();
});
});
or use location.href like,
$(function(){
$('#link1').click(function(){
window.location = $('#link2').attr('href');
});
});
Also, Hoffman gave the answer for the same issue
Try this:
$(document).ready(function()
{
$("#link1").click(function()
{
$('#link2')[0].click();
});
});

How to close the current element on click

This is very silly question. But i am new to html, jquery. Can anyone give me the solution on this.
How to close the element on click
<a>test</a>
When i click test it show close/hide()
I tried as this.hide() onClick but it didnt work anymore.(<a onclick='this.hide()'>test</a>)
In the click handler this will be the native DOM element which does not have the hide() method - you need to turn it into a jQuery object. Try this:
test
$('.foo').click(function(e) {
e.preventDefault();
$(this).hide();
});
Couple of notes; firstly using onclick attributes is outdated. If you use jQuery, use it to hook up your events.
Secondly, a elements must have a href or name attribute, and as such you'll need to use event.preventDefault() to stop the default behaviour when it's clicked on.
.hide() is a method provided by jQuery, it is not available in the dom element.
<a onclick='$(this).hide()'>test</a>
Try like this using jquery:
<a class"some_link">test</a>
$(function(){
$(".some_link").click(function(){
$(this).hide(); // $(this).remove(); to remove
});
});

jQuery keypress event not firing

I have a form that when the user hits edit, it changes the field from text to a textbox with a class of cat_name_edit.
The following code does not trigger when pressing any key in the textbox. Could it have something to do with the fact that I've already changed the text into a textbox?
$(".cat_name_edit").keypress(function() {
alert("hi");
});
I've also tried .click() and .keydown() with no luck. Any ideas?
Ok, apparently I had to use .live()
I think the elements are not present on page load so the events are not attached. Try using jQuery live.
$(".cat_name_edit").live('keypress', (function() {
alert("hi");
});
I put this into a fiddle keypress() works fine:
$(".cat_name_edit").keypress(function(e) {
$(this).replaceWith("<textarea class=\"cat_name_edit\"></textarea>");
$("textarea.cat_name_edit").focus();
});
keyup() would have worked too.
I've also gone to the liberty of making the function that replaces the input with a textarea.
or you could also use EventDelegation. Attach your click event to the parent which contains these textboxes, and in the function check if the target that was clicked has the class cat_name_edit and then perform your operation.

Categories

Resources