Reputation: 1740
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
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
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
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
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