0

It works if I use onclick event on button but if I use jquery it is not working.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <script src="js/jquery.min.js"></script>
        <script>
            $("#btn_skr").click(function(){
                alert();
            })
         </script>
     </head>

     <body>
         <button id="btn_skr">Sign in as seeker</button>
     </body>
</html>
phpNoob
  • 249
  • 1
  • 5
  • 18
  • 1
    ___"jQuery - document ready handler"___ - just google it – Pranav C Balan Jan 09 '17 at 05:50
  • Move the script to *after* the element it tries to access, and/or wrap that code in a document ready handler. (This *must* be a duplicate of a bunch of other questions - I'll see if I can find one.) – nnnnnn Jan 09 '17 at 05:51
  • 2
    @PranavCBalan is right. put the script tag after the element or use the document ready event https://learn.jquery.com/using-jquery-core/document-ready/ – Tal87 Jan 09 '17 at 05:51
  • 1
    you have to write click event inside dom ready event. here your script is executing before the dom load, I mean your script is searching for btn_skr id before the html renders. – Ashish Panchal Jan 09 '17 at 05:52

1 Answers1

0

You have to right inside ready function then it will work

$(function(){
 $("#btn_skr").click(function(){
      alert(1);
    })
 });
Sharmila
  • 725
  • 4
  • 11
  • Actually, the problem is trying to fetch the button before the element is loaded... In the snippet it's working because the snippet code always execute after the markup – Pranav C Balan Jan 09 '17 at 05:54
  • @Sharmila : here the problem is not in jQuery, Its just that the script is executing and searching for the button element before it renders. – Ashish Panchal Jan 09 '17 at 05:55
  • Yes guys i agree. I can reproduce his issue in plunker. When i right within document ready the problem gets resolved. – Sharmila Jan 09 '17 at 05:58