Jump to content

Recommended Posts

hey guys, im trying to find the value of a radio button group, with id / name of 'chart'.

 

all the examples i can find online are like:

 

var val = 0;

 

for( i = 0; i < document.myform.mygroup.length; i++ )

{

if( document.myform.mygroup.checked == true )

val = document.myform.mygroup.value;

}

alert( "val = " + val );

 

which is fine, except i'd rather not base it of of the name of the radio, and the items arent in a form. im learning js still so im not perfect at it but here is kinda what i need:

 

for (var i=0; i<document.getElementById('chart').length; i++) {

if (document.getElementById('chart').checked) {

var chart = document.getElementById('chart').value;

break;

}

}

 

alert('chart - '+chart);

 

obv im having issues with the ('chart') part but i tried ('chart').value and ('chart')..value... but to no avail

 

this is the radio setup

 

 

<td align=center class=headercell><input type=radio name=chart id=chart value=steps {$step_c} onmouseup="Updatechart_two('steps')">Step Comparison</td>

<td align=center class=headercell><input type=radio name=chart id=chart value=thismonth {$thismonth_c} onmouseup="Updatechart_two('thismonth')">Current Month</td>

<td align=center class=headercell><input type=radio name=chart id=chart value=lastmonth {$lastmonth_c} onmouseup="Updatechart_two('lastmonth')">Last Month</td>

<td align=center class=headercell><input type=radio name=chart id=chart value=alltime {$alltime_c} onmouseup="Updatechart_two('alltime')">All-time Monthly</td>

 

 

the reason for {$var} is smarty checked/unchecked.. but that is a non issue.

 

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/158785-solved-radio-button-value-no-form/
Share on other sites

You can't use getElementById() in this case because an id is a unique identifier.  So, JavaScript expects that you're referencing only one element when you use an id.

 

You'll need to grab a hold of the radio buttons by name.  It's not too hard to do:

var radioArray = new Array();
var inputs = document.getElementsByTagName('input');
var chartOutput = '';

for(var i = 0; i < inputs.length; i++)
{
   if(inputs[i].name == 'chart')
   {
      radioArray.push(inputs[i]);
   }
}

/* now you can check to see which button has been pressed */

for(var j = 0; j < radioArray.length; j++)
{
   if(radioArray[j].checked)
   {
      chartOutput += " " + radioArray[j].value;
   }
}

alert("Chart -" + chartOutput);

 

So, you'll need to run through all of the inputs on the page and store those that represent the radio buttons.  Then, you'll need to go through that grouping and obtain the value of the checked button.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.