Get Radio Button value in Foreach-JS

Asked

Viewed 1,969 times

1

I’m trying to get the value of radio in the JS using foreach

foreach($produtos as $produto){
 <input type="radio" id="<?php echo trim($produto->CODIGO); ?>" name="plano_ouro" value="<?php echo trim($produto->CODIGO); ?>">
}

in the JS I don’t know how to take what I selected.

thank you.

  • http://meta.pt.stackoverflow.com/a/1911/101 e http://meta.pt.stackoverflow.com/a/851/101

2 answers

3


Here is the solution:

document.querySelector('input[name="plano_ouro"]:checked').value;
  • Thank you @Antony

1

The @Antony response is correct. It follows the use with jQuery:

$('input[name="plano_ouro"]:checked').val().
  • Although your answer is correct it is unnecessary to use jQuery for something pure JS does with the same simplicity. Still I give +1

  • @Sergio, just for the record. I always try to use pure JS, but depending on the implementation, jQuery is more "legible" within a code full of jQuery. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.