user2104391
user2104391

Reputation: 413

JQuery - Dynamic Img Src Attr Change

Could somebody Explain, What is the Problem with this Code. Once the html is opened in Web Browser, the image is not getting dynamically changed at interval periods.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blinking Text</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    var x;
        setInterval(function() {
                if(x == 0) {
                        $('blinking').attr('src', "http://www.example.com/images/banner1.png");         
                        x = 1;
                } else  {
                    if(x = 1) {
                        $('blinking').attr('src', "http://www.example.com/images/banner2.png");             
                        x = 0;
                    }
                }
        }, 750);
});
</script>
</head>
<body>  
    <div class="blinking"><img src="http://www.example.com/images/banner1.png"></img></div>
</table>    
</body>
</html>

Upvotes: 0

Views: 2464

Answers (4)

TheCodeDestroyer
TheCodeDestroyer

Reputation: 762

This $("blinking") doesn't mean anything to jQuery it thinks you are looking ro and element blinking if you are selecting class you need to specify its prefix selector example:

//CLASS
$(".blinking")
//ID
$("#blinking")

I suggest you to read this

Upvotes: 1

$('div.blinking img');

$('div.blinking') --> div with class blinking.

$('div.blinking img'); --> image whose parent is div with class blinking.

Upvotes: 1

U.P
U.P

Reputation: 7442

The problem is with jQuery selector $('blinking')

If you are selecting by class it should be $('.blinking img'). Notice .

Upvotes: 2

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You forget to add class selector for image.

$('.blinking img')

$('.blinking img') means you are selecting image whose parent is blinking div.

Upvotes: 3

Related Questions