2

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?

user30424
  • 147
  • 1
  • 10
  • 1
    Consider not using the `onclick` attribute and use `addEventListener` instead. – evolutionxbox Aug 30 '20 at 15:08
  • 1
    Does this answer your question? [How to pass an event object to a function in Javascript?](https://stackoverflow.com/questions/1276870/how-to-pass-an-event-object-to-a-function-in-javascript) – evolutionxbox Aug 30 '20 at 15:09

3 Answers3

1

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">
Sascha
  • 4,576
  • 3
  • 13
  • 34
1

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/

aria T
  • 89
  • 6
0
document.querySelector('input[type="radio"]').addEventListener('click', function() {
    go_to_url('OPT3' , this.value);
});
smunteanu
  • 422
  • 3
  • 9
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 30 '20 at 17:26
  • Yeah I know but I was in a hurry and wanted to finish the number of answers I planned to give @MarkRotteveel – smunteanu Aug 30 '20 at 18:35