1

on a webpage there is a "show more" button when I click inspect element appears

<input name="" id="" onclick="GetComments();" class="showMore showMoreNew" value=" المزيد" type="submit">

I want to click it using JavaScript

4
  • Where is your GetComments() function? Commented Jun 29, 2016 at 2:59
  • 1
    Just invoke GetComments(); function... Commented Jun 29, 2016 at 3:07
  • If you want to programmatically execute a click event, you can use $('.showMore.showMoreNew').trigger('click') Commented Jun 29, 2016 at 3:09
  • 1
    I see that you’ve got an onclick handler on a type="submit" button. Therefore, this is related: Submit and onclick not working together. Commented Jun 29, 2016 at 3:14

2 Answers 2

6

I used

document.getElementsByClassName("showMoreNew")[0].click();

and it works.

Sign up to request clarification or add additional context in comments.

Comments

-2

JQuery solution not work under certain condition, below solution always works.

document.getElementById("xxx").click();

2 Comments

Except when the element has no id attribute
it seems the OP does not have control of the source code of the page. He can't add an id unless he were to add it through JS or through the developer console. If through JS, he'd have to select it first and if through the developer console, it's more work than just using a different selector. (Also, if using the console, your answer would need to explain how to do that before it works)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.