12

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>
0

5 Answers 5

33

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

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

4 Comments

shall i know why [0] after id? what is get(0)?
get(0) is same as [0]. Why[0] after id because- $('#about') returns a jQuery object. So a click event on this will fire a jQuery click handler, if defined. $('#about')[0] will return a javascript object and click() on this will simulate actual click.
It worked for me. Anyway I could not understand the concept of "will fire the click handler, while ***** would fire actual click."
@ShaunakD Thanks bro searching for this for a long time
3

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>

1 Comment

You can not use javascript to fire click event - You can stackoverflow.com/q/2381572/3639582.
1

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";

Comments

0

If you want the page to go to next.php without clicking then place the window.location directly into $(document).ready(function() { });

Click() function is used when you want to trigger click event. ie. when you want to trigger some action when anchor tag is clicked.

<script>
   alert("hai");
   $(document).ready(function() { 
      window.location = 'next.php';  //If you wish the page to automatically go to next page on page load.
      $('#about').click(   // If you wish to do something clicking anchor
        function(){
           alert("Hello");
        }); 
      });
</script>

Comments

0
$(document).ready(function() { $('#about').trigger('click'); });

Updated:

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

1 Comment

no respond for this code, it shows normal, after click only it goes next page.

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.