Skip to content
Advertisement

How do I pass the name of the radio button to the Javascript for the onclick event?

I have an html table having n rows. Each row has a name and 2 radio buttons. If I click a radio button, I want to show an alert that states the count of votes. How should I pass the name of the radio button to the Javascript and how should I do the counting?

Here’s the PHP part:

list($cols,) = $xlsx->dimension();
$var=0;
foreach( $xlsx->rows() as $k => $r) {
    $var++;
    echo '<tr>';
        echo '<td>'.$k.'</td>';
        echo '<td>'.( (isset($r[0])) ? $r[0] : '&nbsp;' ).'</td>';
        echo '<td><Input type = "Radio" Name ="vote'.$var.'" value= "pacada"  onclick = "AddPacada()"></td>';
        echo '<td><Input type = "Radio" Name ="vote'.$var.'" value= "toledo"  onclick = "AddToledo()"></td>';
    echo '</tr>';

and here’s the Javascript part:

function AddPacada(){
if(document.tally.pacada.checked == true){
    alert("You have clicked on Pacada.");
}
}function AddToledo(){
if(document.tally.toledo.checked == true){
    alert("You have clicked on Toledo.");
}
}

Advertisement

Answer

You just pass it as an argument:

... onclick = "AddPacada(this)" ...
                         ^^^^

/* And in your function: */

function AddPacada(radio) {
    alert(radio.value);
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement