5

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 class="myLink" href="http://yahoo.com"> CLICK HERE NOW </a>

and I have this right at the top of my page header:

    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

EIDT:

i've tried this as well and still doesn't work:

<script type="text/javascript">
$(document).ready(function(){
    $('.myLink').trigger('click');
    });
</script>
2
  • 1
    you cannot click it because you have not defined click method for the link tag . Commented Oct 17, 2013 at 11:56
  • Most likely, no click event is bound to the link. It IS clickable however, if there's a href in there it will follow that link. Commented Oct 17, 2013 at 12:15

7 Answers 7

16

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.

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

3 Comments

I've tried using your code and it didn't work! could you please provide a jsfiddle ?
This did it mate. Great. appreciate it.
one more question, how can I use the same method to auto click on an image like this: <img class="close" src="images/closey.gif" style="display: inline;"></img> ?
10

getElementsByClassName 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 Comments

+1 for the $('.some-iclass').click(); due to compatibility with older browsers, e.g. ie7+
could you please provide a working jsfiddle as I cannot get this to work!
Fiddle. Your tests with Yahoo.com can't work because Yahoo doesn't want to be embedded in a frame (cross origin settings).
2
$(document).ready(function(){
    $(".some-iclass").trigger('click');
    });

Comments

2

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();

Comments

2

for auto-clicking a button or a link

"<"body onload="document.getElementById('some-class')[0].click()" ">"

this works...:)

Comments

1

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.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.