I have many switches that capture the value from my database, and if it is set to "1", I set the switch.
If the switch is set and the user clicks it again, I still want to clear this button. Does anyone have an idea?
$radio1 captures data from the 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 Answer slightly modified:
I changed 2 lines of code that worked a little better for me. But overall, Varuna's answer was PERFECT!
$('input[type="radio"]').click(function(){
var $radio = $(this);
if ($radio.data('waschecked') == true)
{
$radio.prop('checked', false);
$radio.data('waschecked', false);
}
else
{
$radio.prop('checked', true);
$radio.data('waschecked', true);
}
$radio.siblings('input[type="radio"]').data('waschecked', false);
});
bryan source
share