What is wrong with this jQuery example?

The following jQuery example should put some text in a div, but it is not. I tried Firefox, Google Chrome, and Internet Explorer.

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script> <script language="javascript"> $(window).load(function() { $('adiv').html('<p>hello world</p>'); alert('done'); }); </script> </head> <body> <div id="adiv"> </div> </body> </html> 

Sorry, this might be stupid, but I'm stuck.

+4
source share
6 answers

change $('adiv').html('<p>hello world</p>'); on the

 $('#adiv').html('<p>hello world</p>'); 
+10
source

You do not select anything in your selection function

Immediately after opening $( you need to use a valid CSS3 selector. Only the line will select nothing except the HTML element ( table , div , h2 )

You must specify it with . or # to signal both the class name and the identifier.

+1
source

$('adiv') should be $('#adiv') .

Unlike Prototype, in jQuery you specify a CSS selector, not just a string that is implicitly defined as an identifier. I forget about it from time to time.

0
source

As mentioned in e-turhan, you need # before adiv in $() , otherwise it will not be an id selector. It is also always better to call these .load() events inside the .ready() jQuery event, whose famous label is $(function() { //execute when DOM is ready }); . In your case:

 $(function(){ $(window).load( function(){ $('#adiv').html('<p>hello world</p>'); } ); 
0
source
 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script> <script language="javascript"> $(window).load(function() { $('#adiv').html('<p>hello world</p>'); alert('done'); }); </script> </head> <body> <div id="adiv"> </div> </body> </html> 

Paste this code instead of ur query

Will work

0
source

Try $ (document) .ready (function () ... instead of $ (window) .load (function () ...

-1
source

Source: https://habr.com/ru/post/1307284/


All Articles