Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Toggle only 1 item and the others not clicked

Ok so i have this Jquery that will toggle some radio buttons on the page. I want it so when someone clicks on one of the radio buttons the others are not clicked. Currently the way i have it they can select 3 at a time and I need it to act like a regular radio button that one is clicked and the others are not

jQuery:

$(function () {
  $('.box-ul .btn .dot').click(function () {
    $(this).toggleClass('clicked');
  });
});

HTML:

<div class="box-ul box-video right">
    <ul>
        <li><span class="yes"></span><p>Easy to Use</p></li>
        <li><span class="yes"></span><p>Step by Step<br /><span>details instructions</span></p></li>
        <li><span class="yes"></span><p>Setup time<br /><span>under 30 mins</span></p></li>
        <li class="btn btn-expanded">
          <div class="btn-t">
            <span class="dot left"></span>
          <p class="left">Expanded<br />
            <span>This is the extend button It has space for slightly more info.</span>
          </p>
            <div class="cl">&nbsp;</div>
          </div>
        </li>
        <li class="btn">
          <div class="btn-t">
            <span class="dot left"></span>
            <p class="left">Green Doggies</p>
            <div class="cl">&nbsp;</div>
          </div>
          </li>
        <li class="btn">
          <div class="btn-t">
            <span class="dot left"></span>
            <p class="left">Email Support</p>
            <div class="cl">&nbsp;</div>
          </div>
        </li>
    </ul>
</div>
like image 755
Matt Elhotiby Avatar asked Jul 18 '10 20:07

Matt Elhotiby


People also ask

How to use jQuery click toggle?

The jQuery click toggle can be performed by using the toggle function, toggle () function is a built-in function in jQuery, which is used to toggles the two or more function execution on every click. This is a guide to jQuery click toggle. Here we discuss the Introduction, syntax, examples with code implementation.

What is toggle () method in CSS?

Definition and Usage The toggle () method toggles between hide () and show () for the selected elements. This method checks the selected elements for visibility. show () is run if an element is hidden. hide () is run if an element is visible - This creates a toggle effect.

How to toggle between hide () and show () for selected elements?

The toggle () method toggles between hide () and show () for the selected elements. This method checks the selected elements for visibility. show () is run if an element is hidden. hide () is run if an element is visible - This creates a toggle effect.

How to show or hide a specified element using jQuery?

The toggle method. The toggle method of jQuery will hide specified visible element and display the hidden elements. Use toggle method if you need to allow users show or hide any elements like div, menu, paragraphs etc. in your web pages by giving a switchable option.


1 Answers

Use .removeClass() class on all the matching elements that aren't the one you clicked on first, like this:

$('.box-ul .btn .dot').click(function () {
  $('.box-ul .btn .dot').not(this).removeClass('clicked');    
  $(this).toggleClass('clicked');
});​

You can test it here. In this case we're selecting all the other matching '.box-ul .btn .dot' elements, filtering out the one we clicked on with .not(), and doing a .removeClass() on them. The last part is still a .toggleClass() so you can de-select via clicking on the active one as well. If you don't want to be able to de-select (even more like a radio button) then just change that to .addClass().

like image 63
Nick Craver Avatar answered Oct 27 '22 19:10

Nick Craver