3

first time posting here. I'm a beginner in jquery and i ran into some grey area. Hopefully i can find my answer here and learn from it also :)

So i have a let's say 10 different div. All has the same class. And everytime I click on the div it has to add another class (in this case background-color in css). For that I have this:

$(document).ready(function() {
$(".menucardmenu").click(function(){
        if($(this).hasClass("menucardmenu")) {
               $(this).addClass("backgroundmenucard");
    }
    else {
        alert ("condition false");
    }
  });
});

But the question now is, how can i make that only one div can have that background-color (in my case backgroundmenucard). Depending one which div the user click, that div will have the background-color, and the previous div (that was clicked) should reset it back to normal. I can do it with this right?:

$(this).removeClass("backgroundmenucard");

does anyone know the answer to this???

Regards, Andrew

2
  • I think the "else" block will never execute...am I wrong? Commented Jun 11, 2011 at 19:45
  • @Sayem no, you are not wrong. You might even say you're right ;-) Commented Jun 11, 2011 at 19:47

5 Answers 5

4

try the following:

$(".menucardmenu").click(function(){
    $(".backgroundmenucard").removeClass("backgroundmenucard");
     $(this).addClass("backgroundmenucard");
  });

Demo: http://jsfiddle.net/r2Sua/

(I remove the if because it's useless in this case)

Sign up to request clarification or add additional context in comments.

1 Comment

This was exactly what I needed. Thx you very much. Yes I was looking into the if-else statement so I used it for the basic. After that I was unsure what the purpose was since I it already works
2

Remove from all...

$(".menucardmenu").removeClass("backgroundmenucard");

Then add to this

Comments

1
$(function() // shorthand for document ready
{
    var $divs = $('div.menucardmenu'), // standard jQuery "cache" idiom
        BG_CLASS = 'backgroundmenucard'; // stay DRY, less prone to typos

    $divs.live('click', function() // use .live to bind only 1 event listener
    {   
        // remove the class from all divs
        $divs.removeClass(BG_CLASS);

        // add the class to this div
        $(this).addClass(BG_CLASS);
    }
  });
});

The if($(this).hasClass("menucardmenu")) check is completely unnecessary since you're already selecting elements which have that class. It will always evaluate to true.

Comments

0
$('.menucardmenu').click(function(){
 $('.menucardmenu').removeClass('backgroundmenucard');
 $(this).addClass('backgroundmenucard');
});

Comments

0

Another option would be:

$(".menucardmenu").not(this).removeClass("backgroundmenucard");
$(this).addClass("backgroundmenucard");

This way you don't remove and add the class to the specific (this) element

1 Comment

You're adding the class to this no matter what, so it really doesn't make a difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.