Search code examples
javascriptdom-events

Check if a radio button is on


I am trying to check whether a radio button has been clicked on my page but for some strange reason it not acting the way I expect it to.

In my .js I have:

function radio_is_on() {
    var elementId = document.getElementById('delete');
    if (elementId.checked == "on" ){
    alert('I am here');
    }
}

In my html I have:

<input type="radio" id="some"  name="radio1" value="someVal" >
<input type="radio" id="delete"  name="radio2" value="delete" >
<input class=b1 type="submit" name="ok" value="Go" onclick="radio_is_on();">

Solution

  • Try this:

    function radio_is_on() {
        var elementId = document.getElementById('delete');
        if (elementId.checked){
        alert('I am here');
        }
    }
    

    BTW: If you are not using the same name at least twice, you might want to use checkboxes.