Abhinav
Abhinav

Reputation: 1740

Alert Box does not Pop up

I am new to jQuery and presently reading jquery for dummies,and was trying to execute first example of the book:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”  
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>  
<html>  
<head>  
<title>My Test Page</title>  
<script type="text/javascript" src="js/jquery-1.7.2.js">  
$(document).ready(function(){  
alert(jQuery(‘img’).attr(‘alt’));  
});  
</script>  
</head>  
<body>  
<p>This is my test page.</p>  
 <img src= "images/home.gif" height="28" width="28" alt="This is a test  
image.">  
</body>  
</html>   

But after executing this example,the alert box does not pop-up.

Upvotes: 0

Views: 1170

Answers (4)

gdoron
gdoron

Reputation: 150313

You're using weird quotes (‘ ’): alert(jQuery(‘img’).attr(‘alt’));

alert(jQuery('img').attr('alt')); 

Or with double quotes:

alert(jQuery("img").attr("alt")); 

And you can't write javascript inside a script tag with src attribute:

<script type="text/javascript" src="js/jquery-1.7.2.js"> </script>
<script type="text/javascript">
    $(document).ready(function(){
        alert(jQuery('img').attr('alt'));
    });
</script>  

Upvotes: 1

John Green
John Green

Reputation: 13445

You have multiple issues going on.

First off, your example shows 'smart quotes' (‘ / ’ ). These aren't real quotes as understood by anything in the computing world.

Secondly, you are attempting to utilize script tags incorrectly.

You need to break them into multiple tags, one for the embed, and one for the inline:

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    alert(jQuery('img').attr('alt'));
});
</script>

Upvotes: 0

Johan
Johan

Reputation: 35223

Try changing

<script type="text/javascript" src="js/jquery-1.7.2.js">
$(document).ready(function(){
alert(jQuery(‘img’).attr(‘alt’));
});
</script>

to

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    alert(jQuery(‘img’).attr(‘alt’));
});
</script>

Upvotes: 0

Frederick Behrends
Frederick Behrends

Reputation: 3095

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
    alert($('img').attr('alt'));
  });
</script>

This should do the trick.

Upvotes: 1

Related Questions