Reputation: 1779
Good Day.
I am using the following click function with jQuery
$(function () {
$("#areas ul a").click(function (event) {
event.preventDefault();
$("#areas ul li a").removeClass('activeAreas');
$(this).addClass('activeAreas');
$(".hidden").hide(200);
$($(this).attr("href")).show(500)
});
});
What what I would like to do is in that loop, to check whether a specific element is active, then act upon that:
Eg.
$("#areas ul a").click(function (event) {
event.preventDefault();
$("#areas ul li a").removeClass('activeAreas');
$(this).addClass('activeAreas');
$(".hidden").hide(200);
$($(this).attr("href")).show(500)
//I'm adding this... but it is not working
if($(this).attr(href) = 'pretoriaDetail'){
alert('it works');
}
});
How do I get that if statement to work...?
Thanks
Upvotes: 0
Views: 940
Reputation: 2104
$(function () {
$("#areas ul a").click(function (event) {
event.preventDefault();
if(!$(this).hasClass('activeAreas')){
$("#areas ul li a").removeClass('activeAreas');
$(this).addClass('activeAreas');
$(".hidden").hide(200);
$($(this).attr("href")).show(500);
}
});
});
Upvotes: 1
Reputation: 74738
I think you should try this:
var ID = $(this).attr("href");
$('#'+ID).show(500);
Declare a variable and use it as an id.
Then this should be something like this:
$(function () {
$("#areas ul a").click(function (event) {
var ID = $(this).attr("href");
event.preventDefault();
$("#areas ul li a").removeClass('activeAreas');
$(this).addClass('activeAreas');
$(".hidden").hide(200);
$('#'+ID).show(500); // or this way $('[id="'+ID+'"]').show(500);
});
});
Upvotes: 0
Reputation: 27364
If you want to check if class
is applied to DOM then .hasClass
will help.
Official Document : http://api.jquery.com/hasClass/
and one more thing there is error in your code.
if($(this).attr(href) = 'pretoriaDetail'){
alert('it works');
}
must be
if($(this).attr("href") == 'pretoriaDetail'){
alert('it works');
}
because what =
do is assigning value to that while ==
compare it and href must surround in quotation.
Upvotes: 2