Click here to Skip to main content
15,887,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a radio button , from which i pass the value to db , and in another page i fetch the data and display the form back , when the radio button is checked , its getting unchecked on second click, how do i get to uncheck on first click itself?

What I have tried:

<a id="element1" onClick="javascript:changeText(0);docheck(this);">Mark</a>
<input type="radio" id="element11" onClick="javascript:changeText11(1);docheck11(this);" name="test11" checked="checked"><br />
<script>

function changeText11(idElement) {
    var element = document.getElementById('element' + idElement);
    if (idElement === 1 || idElement === 2) {
        if (element.innerHTML === 'Mark') element.innerHTML = 'Unmark';
        else {
            element.innerHTML = 'Mark';
        }
    }
}

var check;

$('input[type="radio"]').hover(function() {
    check = $(this).is(':checked');
});

$('input[type="radio"]').click(function() {
    check = !check;
    $(this).attr("checked", check);
});

var checkedradio;
function docheck11(thisradio) {
    if (checkedradio == thisradio) {
        thisradio.checked = false;
        checkedradio = null;
    }
    else {checkedradio = thisradio;}
}

</script>
Posted
Comments
[no name] 10-Aug-20 9:18am    
var checkedradio; is undefined for the first click?
Member 11755075 10-Aug-20 9:22am    
yeah , what should i do ?
[no name] 10-Aug-20 9:27am    
Remove if (checkedradio == thisradio) in docheck11? I don't get the logic on that.
Member 11755075 10-Aug-20 9:31am    
okay but if i removed it the code doesnt work
Richard MacCutchan 10-Aug-20 9:41am    
What you are using(or trying to use) is a CheckBox, not a Radio button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900