How to get the text of a radio button?

Asked

Viewed 377 times

2

In my code I check the selected radio and would like to get the radio’s text field as well. How should I do?!

No while it mounts the radios according to the values found in the table and shows the name of the fields found.

<?
    while($w_registro = pg_fetch_object($w_queryresultado))
      {                                   
      print('<input type="radio" name="rb_peri" id="id_rb_peri" value="'.$seq_cara.'">');
      print($tx_desc);
      }
?>

Ai when the radio is selected and issued a "GO", it calls the function to check the selected radio.

function radio(){
    w_qtde_rb = document.forms['sai_frm_alte_novo_cara'].rb_peri.length;

    for  (w_i=0; w_i < w_qtde_rb; w_i++)
        {           
         if(document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].checked == true)
            {   
            get_valor= document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].value;
            }     
        }
}

Then I would like to take the name of the field referring to the selected radio. Any idea?

2 answers

3


If you make a small change to your PHP, the radiobutton text will be printed inside a tag (for example: <label>, which by the way is a good one, in terms of usability), just add to your Javascript the highlighted line:

function radio(){
    w_qtde_rb = document.forms['sai_frm_alte_novo_cara'].rb_peri.length;

    for  (w_i=0; w_i < w_qtde_rb; w_i++)
    {           
        if(document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].checked == true)
        {   
            get_valor = document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].value;

            // Adicionar esta linha:
            get_texto = document.forms['sai_frm_alte_novo_cara'].rb_peri[w_i].nextSibling.innerHTML;
        }     
    }
}

The corresponding modification, in PHP, must be the line replacement

print($tx_desc);

for

print('<label for="id_rb_peri">' . $tx_desc . '</label>');
  • Thanks a lot @Ruipimentel helped me a lot!!!

-2

If you prefer to use Javascript you can do so: $("#id_rb_peri"). val(); then you will have the value of it and can do whatever you want !

Browser other questions tagged

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