Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to click an anchor tag from javascript or jquery

Have an anchor tag, trying to click it from the javascript but not responding, while loading the page it should go to next.php without click anchor tag manually.how can I archive this?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="jquery-2.0.3.js"></script>
    <script type="text/javascript">
         alert("hai");
         $(document).ready(function() { $('#about').click(); });             
    </script>
</head>   
<body>
    <div>
         <a href="next.php" id="about">click here</a>
    </div>
</body>
</html>
like image 489
151291 Avatar asked Jun 01 '15 04:06

151291


3 Answers

Use $('selector')[0], as $('selector') returns a jQuery object, so $('selector').click() will fire the click handler, while $('selector')[0].click() would fire the actual click.

$(document).ready(function () {
    $('#about')[0].click();  //$('#about').get(0).click();
});

Demo

like image 94
Shaunak D Avatar answered Sep 19 '22 16:09

Shaunak D


You can not use javascript to fire click event

It should use

<script type="text/javascript">
       $(document).ready(function() { 
             document.location.href='/next.php'
       });
</script>
like image 42
PhuLuong Avatar answered Sep 21 '22 16:09

PhuLuong


Here is the code that can help you

$(document).ready(function() {


  $(document).ready(function() {
    $('a').trigger('click');
  });

})

function abc() {
  alert("");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="abc()">click me not</a>

If you want the recommended way then use this inside $(document).ready

window.location="where ever you want to go";
like image 34
user786 Avatar answered Sep 17 '22 16:09

user786