javascript and this.value of radio button

Viewed 57

I try to get the value of the checked radio button by javascript.

<input value="3" type="RADIO" name="ME" onclick="go_to_url ( 'OPT3' , this.value );">

But JS does not give me any value even the event is where the button is?

Why? How can I get the value of the clicked radio button in a way I do not have to change the function?

3 Answers

Use for your button an id then you can use document.getElementById to get your element and addEventListener to add an click-event. The on-click in your HTML delete.

document.getElementById('btn').addEventListener('click', function() {
    console.log(this.value);
})
<input value="3" id='btn' type="RADIO" name="ME">

This code has no problem. Check this out

<input value="3" type="radio" name="ME" onclick="go_to_url( 'OPT3' , this.value );">

function go_to_url(label, value) {
    console.log(label);
    console.log(value);
}

https://jsfiddle.net/f7gkesxo/

document.querySelector('input[type="radio"]').addEventListener('click', function() {
    go_to_url('OPT3' , this.value);
});
Related