Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio button uncheck on second click

I have a LOT of radio buttons that grab a value from my database and if it is set to "1", I make the radio button checked.

If a radio button is checked, and a user clicks on it again, I still want to be able to clear this button. Does anyone have an idea?

$radio1 grabs data from database and will be either 0, 1, or 2

<input value="1" name="radio1" type="radio"<?php if($radio1==1){echo " checked";} ?>>
<input value="2" name="radio2" type="radio"<?php if($radio1==2){echo " checked";} ?>>

Varun Malhotra's Answer slightly modified: I changed 2 lines of code that worked a little better for me. But overall, Varun's answer was PERFECT!

$('input[type="radio"]').click(function(){
    var $radio = $(this);

    // if this was previously checked
    if ($radio.data('waschecked') == true)
    {
        $radio.prop('checked', false);
        $radio.data('waschecked', false);
    }
    else
    {
         $radio.prop('checked', true);
         $radio.data('waschecked', true);
    }

    // remove was checked from other radios
    $radio.siblings('input[type="radio"]').data('waschecked', false);
});
like image 758
bryan Avatar asked Nov 10 '13 20:11

bryan


People also ask

How do you make a radio button unchecked by clicking it?

To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked.

Can we uncheck radio button?

It is not a mandatory field. Radio button helps in ensuring only one option is selected. However, it doesn't allow user to deselect the option.

Can radio buttons be deselected by default?

Radio buttons are distinct from checkboxes. The user can change options between a set of radio buttons, but it cannot be deselected.

How do I uncheck a radio button in Excel?

How do I uncheck a radio button in Excel? Go to Home –> Editing –> Find & Select –> Selection Pane. Select all the radio buttons that you want to delete (to select multiple radio buttons, hold the control key while selecting) and hit the delete key.


3 Answers

I'll suggest to add a custom attribute to keep track of each radio's previous state like so:

$(function(){
    $('input[name="rad"]').click(function(){
        var $radio = $(this);

        // if this was previously checked
        if ($radio.data('waschecked') == true)
        {
            $radio.prop('checked', false);
            $radio.data('waschecked', false);
        }
        else
            $radio.data('waschecked', true);

        // remove was checked from other radios
        $radio.siblings('input[name="rad"]').data('waschecked', false);
    });
});

You will also need to add this attribute to the initially checked radio markup

<input type="radio" name="rad" id="Radio0" checked="checked" data-waschecked="true" />

JSFIDDLE DEMO

UPDATE:

 $(function(){
        $('input[name="rad"]').click(function(){
            var $radio = $(this);

            // if this was previously checked
            if ($radio.data('waschecked') == true)
            {
                $radio.prop('checked', false);
                $radio.data('waschecked', false);
            }
            else
                $radio.data('waschecked', true);

            // remove was checked from other radios
            $radio.siblings('input[type="radio"]').data('waschecked', false);
        });
    });

But do ensure that you don't have other radio-groups to work with, otherwise you have to provide some attributes to specify these buttons like name prop I focused earlier.

like image 104
softvar Avatar answered Sep 28 '22 04:09

softvar


This should work more generally than the other solutions as it handles cases where all radios are not in the same parent.

var $radios = $('input[type="radio"]');
$radios.click(function () {
  var $this = $(this);
  if ($this.data('checked')) {
    this.checked = false;
  }
  var $otherRadios = $radios.not($this).filter('[name="'
                                               + $this.attr('name') + '"]');
  $otherRadios.prop('checked', false).data('checked', false);
  $this.data('checked', this.checked);
});
like image 44
Bertrand Bordage Avatar answered Sep 28 '22 04:09

Bertrand Bordage


Change event fires before click when you do first click. You can use them. Working with many radio groups and even if you have prechecked radio buttons.

$("input[type=radio]").each(function() {
    var secondClick = true;
    $(this).change(function() {
        secondClick = false;
    });
    $(this).click(function() {
        if (secondClick) {
            $(this).prop("checked", false);
        }
        secondClick = true;
    });
});

JSFIDDLE DEMO

like image 25
mrsnowin Avatar answered Sep 28 '22 02:09

mrsnowin