I am trying to auto click a link using a class name instead of the ID name.
however my approach doesn't do anything!
Here is what I have done:
<script type="text/javascript"> $(document).ready(function(){ document.getElementsByClassName("some-iclass").click(); }); </script> Could someone point me in the right direction please?
EDIT:
I've used the following code and still doesn't work:
<script type="text/javascript"> $(document).ready(function(){ $(".myLink").click(); }); </script> <a href=""> CLICK HERE NOW </a> and I have this right at the top of my page header:
<script src=""></script> <script src=""></script> EIDT:
i've tried this as well and still doesn't work:
<script type="text/javascript"> $(document).ready(function(){ $('.myLink').trigger('click'); }); </script> 27 Answers
here you go:
<script type="text/javascript"> $(function(){ $('.className').trigger('click'); }); </script> hope that helps.
UPDATE:
try:
<script type="text/javascript"> $(function(){ window.location.href = $('.className').attr('href'); }); </script> after your edit, i think this is what you need.
4getElementsByClassName doesn't return an element but a NodeList which may contain more than one element.
You may do this :
document.getElementsByClassName("some-iclass")[0].click(); or if you want to click all elements :
var list = document.getElementsByClassName("some-iclass"); for (var i=0; i<list.length; i++) list[i].click(); But as you use jQuery, it would be simpler to do
$('.some-iclass').click(); but only when the click event handler was added with jQuery (in other cases, like for example in case of an href attribute, use the standard dom functions).
3$(document).ready(function(){ $(".some-iclass").trigger('click'); }); Simple with jquery $(".some-iclass").click();
if you have a lot of elements with this class - point to the wanted element: i.e. $($(".some-iclass")[0]).click();
for auto-clicking a button or a link
"<"body onload="document.getElementById('some-class')[0].click()" ">"
this works...:)
if you want to autoclick a link and you are using jQuery, you could use
$('.yourClass').click(); if you need this to be one link in a collection of multiple links, you could do this:
$($('.yourClass')[0]).click(); Where 0 is the index of the element in the jQuery object.
document.getElementsByClassName('yourClass'); does not work in older browsers so it's best to use jQuery here for cross-browser compatibility.
For me, I managed to make it work that way. I deployed the automatic click in 5000 milliseconds and then closed the loop after 1000 milliseconds. Then there was only 1 automatic click.
<script> var myVar = setInterval(function ({document.getElementById("test").click();}, 500); setInterval(function () {clearInterval(myVar)}, 1000); </script>