Lotherad
Lotherad

Reputation: 125

jQuery .click() does nothing

Let's say that I got code like that:

<div class="div">
  <input type="submit" value="Button" class="button1">
</div>

And then when I put in the console this code:

document.getElemetsByClassName('button1')[0].click();

It does click the button, but when I try the same thing in jQuery with arrays:

array = document.getElementsByClassName('div');
$(array[0]).find('button1').click();

It does not work and does not return any error messages so I don't know what is wrong. Thanks from above for help.

Upvotes: 2

Views: 132

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

The selector you are using is a tag selector. Change it to class selector:

$(array[0]).find('.button1').click();
//----------------^ Add a . here.

I would also better change this to:

$(".div .button1").trigger("click");

Works better this way, using trigger().

Upvotes: 0

Ibrahim Khan
Ibrahim Khan

Reputation: 20750

button1 is class. You should add a dot(.) before button1 to select button1 class like following.

$(array[0]).find('.button1').click();

Upvotes: 2

Related Questions