toggleClass and remove class from all other elements

How can I toggleClass and remove class from all other elements? Consider a div that contains a tags: html:

<div class="size">
   <a href="">blahblah</a>
   <a href="">blahblah</a>
</div>

jQuery:

 $(".size a").click(function(){
 $(this).toggleClass('checked');
 if($(".size a").hasClass('checked')){
     $(this).removeClass('checked');
  }
 })

I want to add class "cheched" to element and remove the class "ckeched" from other elements that have class "checked" . My code remove all classes. How can I add specific class and remove other element's class with click? Thanks in advance


Solution 1:

This will do it

 $(".size a").click(function(){
    $('.size a.checked').not(this).removeClass('checked');
    $(this).toggleClass('checked');
 })

Update

Alternatively you could do

 $(".size").on('click','a', function(){
    $(this).toggleClass('checked').siblings().removeClass('checked');
 })

Solution 2:

Only You need to Use it. :)

<script>
    $(document).ready(function () {
        $('.demo_class').click(function () {
            $(this).toggleClass('active').siblings().removeClass('active');
        });
    });
</script>

Solution 3:

So late but its my answer

$(".size").click(function(){ $('.size').removeClass('checked') //Clear all checked class on .size $(this).addClass('checked') //Add checked class to current clicked .size })

Its cleaner,powerfull and safe.

Solution 4:

I'm assuming that you have a class 'size' that contains a number of elements of which only one should be able to have the class 'checked' at any one time.

$(".size a").click(function()
{
    if($this).hasClass('checked')
    {
        $(".size a").removeClass('checked');
        $(this).addClass('checked');
    }
    else
    {
        $(this).removeClass('checked');
    }
}

Solution 5:

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>On click Change Css</title>
    <link rel="stylesheet" href="css/style.css" >
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script>
        $(document).ready(function(){
            $('.main').click(function(){
                $(this).toggleClass('main1').siblings().removeClass('main1');;
             });
        });
    </script>

</head>

<body>

    <center>
        <div class="main" id="main-box">
            <h2>This is main Heading</h2>
            <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            </p>
        </div>
        <div class="main">
            <h2>This is main Heading</h2>
            <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            </p>
        </div>
        <div class="main">
            <h2>This is main Heading</h2>
            <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
            </p>
        </div>
    </center>

</body>

Css

.main {
  width:300px;
  margin:30px;
  margin:20px 25px;
  text-align:center;
  background-color:#CCC;
  padding:20px;
  display:inline-block;
}
.main:hover {
  cursor:pointer;
}
.main1 {
  width:300px;
  margin:30px;
  margin:20px 25px;
  text-align:center;
  background-color:#CCC;
  padding:20px;
  border:3px solid #036;
}