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

mtesta010
  • 103
  • 1
  • 11

2 Answers2

6

I used

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

and it works.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
mtesta010
  • 103
  • 1
  • 11
-2

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

document.getElementById("xxx").click();
iamnoten
  • 231
  • 2
  • 7
  • 1
    Except when the element has no id attribute –  Jun 29 '16 at 03:08
  • 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) –  Jul 05 '16 at 11:50