Problems
[list][]The code will be in the click function, so the script tags shouldn’t be there.
[]The .slidingDiv won’t work as it is there, you need to use a jQuery selector to select it, and to retrieve the display value of it.
[]The === operator should be used instead of the == one, as the latter leave far too much room for problems to occur.
[]You need to compare the display property of the sliding div with the string ‘none’, so that show variable need to be taken out.
[]Opening braces should be at the end of the line
[]The code in the if statement should be indented, to provide a visual indication of the nesting
[]You don’t need to assign anything to the img variable
[]It’s the image within the clicked link that needs to be changed, which is referred to differently than what you have there
[]The this keyword needs to be a part of the image selector, not with the assignment of the image name
[][/list]
[s][color="red"]<script type="text/javascript">[/color][/s]
if (.slidingDiv [s][color="red"]== show[/color][/s][color="green"])[/color]
[s][color="red"]{[/color][/s]
[s][color="red"]var img = $([/color][/s]'images/clicktoclose.gif'[s][color="red"], this)[/color][/s];
}
else
[s][color="red"]{[/color][/s]
[s][color="red"]var img = $([/color][/s]'images/clicktoopen.gif'[s][color="red"], this)[/color][/s];
}
[s][color="red"]</script>[/color][/s]
Other than that though, there’s no trouble.
Solutions
Let’s put this together so that all of those problems are resolved.
The code will be in the click function, so the script tags shouldn’t be there.
The place where the code should be put is in the place with the ellipses:
$('.show_hide').click(function(){
...
$(".slidingDiv").slideToggle();
});
The .slidingDiv won’t work as it is there, you need to use a jQuery selector to select it, and to retrieve the display value of it.
The sort of jQuery selector you would use is: $(‘.slidingDiv’).css(‘display’)
The === operator should be used instead of the == one, as the latter leave far too much room for problems to occur.
Using type coercive comparisons more commonly leads to problems for beginners and intermediate users, because it’s doing something that they didn’t know about or expect. Using the strict comparison leaves much less room for problems to occur.
You need to compare the display property of the sliding div with the string ‘none’, so that show variable need to be taken out.
The string from the CSS display property that we’re comparing with is ‘none’
Opening braces should be at the end of the line
In some languages it doesn’t matter where you positioning of the opening brace. With JavaScript, something called Automatic Semicolon Insertion means that you will have problems in certain cases when the opening brace is not at the end of the line.[/b]
A video called JavaScript: The Good Parts demonstrates this problem with positioning the brace in a very understandable way, [url=“http://www.youtube.com/watch?v=hQVTIJBZook&feature=player_detailpage#t=1838s”]from 18:38 in to the video.
The code in the if statement should be indented, to provide a visual indication of the nesting
Proper indentation is one of the more important techniques that make your code easily understandable.
You don’t need to assign anything to the img variable
There’s no benefit gained from assigning the jQuery object of the image to a variable.
It’s the image within the clicked link that needs to be changed, which is referred to differently than what you have there
The image files are assigned to the src attribute of the image.
The this keyword needs to be a part of the image selector, not with the assignment of the image name
One way to refer to the image would be $(‘img’, this)
And then you would set the src attribute to the appropriate image.
Result
if ($('.slidingDiv').css('display') === 'none') {
$('img', this).attr('src', 'images/clicktoclose.gif');
} else {
$('img', this).attr('src', 'images/clicktoopen.gif');
}