user3322672
user3322672

Reputation: 153

image change with .attr() doesn't work

I'm trying to change the img on click. this is my html code:

<div id ="Nl">
   <img id="imgNl" src="images/Nl.jpg">
</div>

this is the awnser I found:

<script>
        $('#Nl').click(function(){
            $('imgNl').attr('src', 'images/smiley.jpg');
        });
 </script>

It doesn't work. what did I do wrong?

Upvotes: 0

Views: 82

Answers (3)

rakhi4110
rakhi4110

Reputation: 9281

You forgot the id selector '#'

$('#imgNl').attr('src', 'images/smiley.jpg');

for more info on selectors refer this

Upvotes: 5

vikaskimt
vikaskimt

Reputation: 56

your correct script is :-

    <script>
         $('#Nl').click(function(){
         $('#imgNl').attr('src', 'images/smiley.jpg');
       });
    </script>

Upvotes: 0

Karthick Kumar
Karthick Kumar

Reputation: 2361

<script>
        $('#Nl').click(function(){
            $('#imgNl').attr('src', 'images/smiley.jpg');
        });
 </script>

missed the # for id

Upvotes: 2

Related Questions