user2889836 user2889836 - 1 year ago 77
jQuery Question

Auto click a link by class name using javascript or jquery?

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>

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download