I am currently teaching myself jQuery (started yesterday) and I'm stuck already.. I have this to create small dropdowns that will work relative to each other.
$(document).ready(function(){
var selTxt = 0;
$(".button").click(function(){
alert($(this).prev(".button").attr("id"));
$(".text").slideUp(150);
$(this).next(".text").slideToggle(150,function(){
selTxt = $(this).prev(".button").attr("id");
});
});
});
Those of you that know jQuery will realise it simply expands a textbox when clicking a button and hides all others that are expanded.
Question:
Take a look and see where the alert()
function is called. On that line I wish to run an if statement.. Something like this:
$(".button").click(function(){
if($(this).attr("id") == selTxt){
$(".text").slideUp(150);
$(this).next(".text").slideToggle(150,function(){
selTxt = $(this).prev(".button").attr("id");
});
}
});
Hence I only want it to run if selTxt
doesn't equal the id
of the element that has been clicked. So the $(this) in the if statement (I HOPED) would refer to the .button
being clicked, but guess what: it doesn't! Could anybody inform me why not? I thought because I referenced the .button
with $(this) lower down that I would be able to use it there as well but evidently not.
Good old JSFiddle here.
Thanks in advance.