How can I use javascript to "click" a button that has no id, value, class or name?
The relevant code for the button is:
<div class="classname anotherclassname" title="">
<div>
<button type="submit"><i class="anotherclass anotherclassname"></i>ImAButton</button>
</div>
</div>
I'd give an example of what I have so far, as I know that is simply good etiquette here on stackoverflow, but I don't even know where to start. The only way I presently know how to use javascript to click a button is using this:
document.getElementById("myButtonId").click();
And that doesn't apply here.
If you are okay with only supporting modern browsers and IE8 and above, then you could use document.querySelectorAll
to select the element. Given that the button is the only button of type submit on the page, you could do:
document.querySelectorAll("button[type='submit']")[0].click();
querySelectorAll takes any valid CSS-selector (IE8 only support CSS2 selectors). So if you need to make the selection more specific, you could just make the selector more specific as you would with any CSS-selector. Something like this for example:
document.querySelectorAll(".classname button[type='submit'"])[0].click();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With