views:

77

answers:

5

The following jQuery example should put some text into the div, but it doesn't. 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.

A: 

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

rmarimon
+6  A: 

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

$('#adiv').html('<p>hello world</p>');
e-turhan
And change $(window).load to $(document).ready
James Westgate
+2  A: 

You're not actually selecting anything in your select function

Directly after your $( opening, you need to use a CSS3 valid selector. Just a string won't select anything unless it's a HTML element (table, div, h2)

You need to preface it with a . or a # to signal either a class or ID name.

Bill X
A: 

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

Unlike Prototype, in jQuery you specify a CSS selector, not just a string that is implicitly inferred to be an ID. I find myself forgetting this from time to time.

Nicholas Piasecki
A: 

As e-turhan already mentioned you need # in front of adiv in your $() otherwise this is not a id selector. Also it's always better to call these .load() events inside the .ready() jQuery event whose famous shortcut is $(function() { //execute when DOM is ready }); . In your case:

$(function(){

$(window).load(
 function(){
   $('#adiv').html('<p>hello world</p>');   
 }

);
Zlatev