JavaScript DOM element doesn't display element does not exist in cache,element does not have

I am new in Javascript and I am trying to test following code

<html>
  <head>
  <script src="jquery/jquery.min.js">
  </head>
<body>
  <input type="button" value="Click button">
  <script type="text/javascript">
  $(function(){
    $('input')[0].on('click',function(){
      alert('button clicked');
    });  
   });
  </script>
</body>
</html>

but when i open this html file in browser the button doesn't display

Answer:1

Script tags require a closing tag, which you've omitted:

<script src="jquery/jquery.min.js"></script>
                                   ^ close

By leaving it open, the browser is treating everything after the script tag as the script content.

Also your $('input')[0] isn't right. That is getting the DOM element of the input, which has no jQuery wrapper and no .on() function. If you are trying to match just the first input then:

$('input').first().on('click',function(){
Answer:2

Problems

  1. Script block must be closed which you missed.
  2. Use $('input') instead of $('input')[0], When you use $('input')[0] you get DOM element which doesn't have click method.

Use

<html>
  <head>
  <script src="jquery/jquery.min.js"></script>
  </head>
<body>
  <input type="button" value="Click button">
  <script type="text/javascript">
  $(function(){
    $('input').on('click',function(){
      alert('button clicked');
    });  
   });
  </script>
</body>
</html>
Answer:3

To avoid these kind of confusions, try to separate like below,

In HTML:

<html>
  <head>
  <script src="jquery/jquery.min.js"></script>
  <script src="main.js"></script>
  </head>
 <body>
   <input type="button" value="Click button">
 </body>
</html>

In main.js

$(function(){ $('input').on('click',function(){ alert('button clicked'); });
});

For Demo

Answer:4

I have a string that I'd like to pull some content from using javascript. The string can have multiple forms as follows: [[(a*, b*) within 20]] or [[...(a*, b*) within 20]] where the "..." may or may ...

I have a string that I'd like to pull some content from using javascript. The string can have multiple forms as follows: [[(a*, b*) within 20]] or [[...(a*, b*) within 20]] where the "..." may or may ...

  1. combine match regex
  2. c# combine regex matches

I'm making a chrome extension, and so far I'm just testing some things. The HTML for the extension is this: <!doctype html> <html> <head> <title>Title</title> &...

I'm making a chrome extension, and so far I'm just testing some things. The HTML for the extension is this: <!doctype html> <html> <head> <title>Title</title> &...

  1. html form submit button
  2. html form submit javascript
  3. html form submit post
  4. html form submit on enter
  5. html form submit action
  6. html form submit to email
  7. html form submit without redirect
  8. html form submit json
  9. html form submit without refresh
  10. html form submit event
  11. html form submit redirect
  12. html form submit with header
  13. html form submit prevent default
  14. html form submit to same page
  15. html form submit not working
  16. html form submit button text
  17. html form submit button value
  18. html form submit without button
  19. html form submit new tab
  20. html form submit button outside form

I have been having some trouble in creating a simple notification box. Basically the objective here when I click the submit button input type=button it will display a simple notification box like this....

I have been having some trouble in creating a simple notification box. Basically the objective here when I click the submit button input type=button it will display a simple notification box like this....

I was reading the javascipt code in some application and code was this getTotalFees:function(){ return this.grid &&this.grid.getStore().sum('fees'); } Now i am confused what ...

I was reading the javascipt code in some application and code was this getTotalFees:function(){ return this.grid &&this.grid.getStore().sum('fees'); } Now i am confused what ...

  1. what does return do in python
  2. what does return service requested mean
  3. what does return on equity mean
  4. what does return mean
  5. what does return on assets mean
  6. what does return to sender mean
  7. what does return do in java
  8. what does return of the mack mean
  9. what does return on investment mean
  10. what does return accepted mean
  11. what does return on assets measure
  12. what does return do in javascript
  13. what does return status printed mean
  14. what does return on sales mean
  15. what does return 0 do in c++
  16. what does return of service mean
  17. what does return mean in java
  18. what does return do in c++
  19. what does return to sender processed mean
  20. what does return in transit mean