-1

Very simple code, for some reason nothing I try will work! I have obviously imported Jquery, jqueryUI, ajax, all the things I need imported (more than once!). But for some reason this button does not want to be clicked using Jquery! I got it to work with onClick, but I would like to be using jquery for this. SOS!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="jquery-1.11.3.min.js"></script>

<script>

$("#heyo").click(function(){
alert();
});

$("input").click(function(e){
var id1 = e.target.id;
alert(id1);
 });

$('input[type="button"]').click(function(){
alert();
});

function pie(){
//alert();
}

</script>

<body>
loading...
<input type="button" id="heyo" onclick="pie()" value="ff" />
</body>
sgrutman978
  • 114
  • 8
  • 2
    Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Patrick Evans Oct 19 '15 at 16:16

1 Answers1

2

Wrap you jquery code with $(document).ready(function() {}) to make sure all the DOM objects have been loaded before accessing them with jQuery:

 $(document).ready(function() {

       $("#heyo").click(function(){
         alert();
       });

       $("input").click(function(e){
          var id1 = e.target.id;
          alert(id1);
        });

        $('input[type="button"]').click(function(){
          alert();
          });

   });
KAD
  • 10,375
  • 4
  • 25
  • 60