By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

newbie: reading radio selection

P: n/a
Hello,
I am learning JavaScript. I created radiobutton group:

<input type="radio" name="building_type" value="N">new</input>
<input type="radio" name="building_type" value="O">old</input>
<input type="radio" name="building_type" value="" checked>n/a</input>

and I want to test selection in javascript:

if (document.announcement.building_type.value != "") ...

My problem is that - whatever is selected -
document.announcement.building_type.value is undefined.
I expected "N", "O", "".
Please help.
/RAM/
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
SAM
r_********@poczta.onet.pl a écrit :
Hello,
I am learning JavaScript. I created radiobutton group:

<input type="radio" name="building_type" value="N">new</input>
<input type="radio" name="building_type" value="O">old</input>
<input type="radio" name="building_type" value="" checked>n/a</input>

and I want to test selection in javascript:

if (document.announcement.building_type.value != "") ...

My problem is that - whatever is selected -
document.announcement.building_type.value is undefined.
to get value of any kind of form's element

function getElementValue(elemt) {
if(elemt.tagName == 'input' && elemt.length)
{ // elemt is radios or checkboxes collection
for(var i=0, L = elemt.length; i<L; i++) {
if(elemt[i].checked) {
elemt = elemt[i];
break;
}
}
}
return elemt.value;
}
if (getElementValue(document.announcement.building_ty pe) != "")
--
sm
Jun 27 '08 #2

P: n/a
On Jun 21, 4:46 pm, r_ahims...@poczta.onet.pl wrote:
Hello,
I am learning JavaScript. I created radiobutton group:

<input type="radio" name="building_type" value="N">new</input>
<input type="radio" name="building_type" value="O">old</input>
<input type="radio" name="building_type" value="" checked>n/a</input>

and I want to test selection in javascript:

if (document.announcement.building_type.value != "") ...

My problem is that - whatever is selected -
document.announcement.building_type.value is undefined.
I expected "N", "O", "".
Please help.
/RAM/
Look at the DOM method getElementsByName

http://www.w3schools.com/htmldom/met...entsbyname.asp

You want something like,

var obj = document.getElementsByName('building_type');

then you should see that obj[0].value == "N" and obj[1].value == "O"
and obj[2].value == "".

Good luck.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.