tags:

views:

23

answers:

4

Hello,

When I click on the word Directory, the up arrow image must change to down, and vice-versa when clicked again.

In this code, when the page loads, toggle is triggered.

$('#direct').click(function(){
                                                                $('#arrow').attr('src','images/down_arrow_black.gif');
                                                                });


<div id="direct"><img src="images/up_arrow_black.gif" id="arrow" />Directory</div>

Thanks Jean

A: 

if your jQuery wrapped in the following?

$(document).ready(function() {
    ....
})
David
yes, else a jscript error would show up
Jean
A: 

Wrap it in

$(document).ready(function() {
    // your toggle code
});
XIII
+1  A: 

I'd use the .toggle() method to make the toggle work, and $() to be sure it loads correctly:

$(function() {
    $('#direct').toggle(function(){
        $('#arrow').attr('src','images/down_arrow_black.gif');
    }, function() {
        $('#arrow').attr('src','images/up_arrow_black.gif');
    });
});
MvanGeest
I have to wait 8 mins to accept your answer, silly
Jean
this is faster than `.toggle()`, http://stackoverflow.com/questions/3069458/jquery-toggle-triggering-on-page-load/3069534#3069534
Reigel
Could be faster indeed :)
MvanGeest
A: 

if you are using jQuery 1.4

you can do it this way...

$('#direct').click(function(){
   $('#arrow').attr('src',function(i,src){            
        return src.indexOf('down')?'images/up_arrow_black.gif':'images/down_arrow_black.gif';
   });
});
Reigel