Currently I have a heading with a downwards pointing icon. If the header is clicked, I want the image to change to an upwards one.
I have tried using the “?” operator to query if it is, but I am not 100% sure how it works. I’m using this code at the moment.
// Toggle message_body
$(".message_head").click(function(){
var id = $(this).attr("id");
$(this).next(".message_body").slideToggle(500);
$(".icon[id=" + id + "]").attr("src", "../images/admin/Symbol_Down.png" ? : "../images/admin/Symbol_Up.png");
return false;
});
I know this code does not work. Could someone please show me how? Thanks!
This will do the trick. Note that you need to do the comparison within the terenary operator. If you just do
"something" ? "blah" : "result, it will always be true, because non-null values are always true.I just realized one fundamental issue with your HTML if your JavaScript css selector is accurate. You are asking for .icon[id=x] and using the ID attribute from the .message_head class to find the ID of the icon. For this to work, you would have to have the same ID for both, which is invalid HTML. What I imagine your HTML looks like is this:
This is a nono. What you can do is this: