DELION
DELION

Reputation: 127

Check only one checkbox and unchek others

Im trying to check only one checkbox at the time and others uncheck , but when i want to check new chekcbox then pervious will uncheck and so on.

i tried this code:

JS file

$('input[type="checkbox"]').click(function() {
$(".check_class").attr("checked", false);
$(this).attr("checked", true);    
});

HTML file

    <ons-list-header>Takistused</ons-list-header>
    <ons-list-item modifier="tappable">
      <label class="checkbox checkbox--noborder checkbox--list-item">
        <input type="checkbox" id="example">
        <div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark"></div>
        vihmaveerenn
      </label>
    </ons-list-item>
    <ons-list-item modifier="tappable">
      <label class="checkbox checkbox--noborder checkbox--list-item" id="test">
        <input type="checkbox" id="example">
        <div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark" ></div>
        äärekivi
      </label>
    </ons-list-item>
    <ons-list-item modifier="tappable" >
      <label class="checkbox checkbox--noborder checkbox--list-item" id="test1">
        <input type="checkbox" id="example">
        <div class="checkbox__checkmark checkbox--noborder__checkmark checkbox--list-item__checkmark"></div>
        munakivi tee
      </label>
    </ons-list-item>
  </ons-list>

What im doing wrong? i have tried different approaches, but it just wont work in live use.

Upvotes: 6

Views: 35943

Answers (4)

Vasim Shaikh
Vasim Shaikh

Reputation: 4522

If you want to do this using class. You have add class and remove except current element.

jQuery(document).on('click', '.esg-filterbutton', function() { 
    jQuery(this).addClass('selected');     
    jQuery('.esg-filterbutton').not(this).removeClass('selected');  
});

Upvotes: -1

Bhanu Pratap
Bhanu Pratap

Reputation: 1761

 $(function () {
        $('input[type=checkbox]').click(function () {
             var chkBox = document.getElementById('<%= chkBranchRoleTransaction.ClientID %>');
            var chks = chkBox.getElementsByTagName('INPUT');
             for (i = 0; i < chks.length; i++) {
                 chks[i].checked = false;
             }
             $(this)[0].checked = true;
         });
    });

Upvotes: 0

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93571

You should really use radio buttons for this type of functionality, however you can simply exclude an item from a set of matches with not like this:

$('input[type="checkbox"]').click(function() {
   $('input[type="checkbox"]').not(this).prop("checked", false);
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/4n2e31oq/

Note you need to use prop and not attr for certain properties like checked.

You can make this a little more efficient by caching the selection of checkboxes:

var $checks = $('input[type="checkbox"]');
$checks.click(function() {
   $checks.not(this).prop("checked", false);
});

http://jsfiddle.net/TrueBlueAussie/4n2e31oq/1/

Upvotes: 5

kapantzak
kapantzak

Reputation: 11750

Try the .not(this) like bellow:

$(document).on('click', 'input[type="checkbox"]', function() {      
    $('input[type="checkbox"]').not(this).prop('checked', false);      
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="one" />
<input type="checkbox" id="two" />
<input type="checkbox" id="three" />
<input type="checkbox" id="four" />

Upvotes: 24

Related Questions