/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help Please

I am trying to get the value from a series of radio buttons, and my method worked for the first set of 3, but when I repeated the procedure for the 2nd set of 3 radio buttons it didn’t work because of the “.length”
[B]
Working Code:[/B]

[code]
for (var x=0; x<Form.A111_answer.length; x++)
{
if (Form.A111_answer[x].checked)
{
var A111_response=Form.A111_answer[x].value;
}
}
[/code]

[B]Not-Working Code [/B](immediately follows the previous code):

[code]
for (var x=0; x<Form.A122_answer.length; x++)
{
if (Form.A122_answer[x].checked)
{
var A122_response=Form.A122_answer[x].value;
}
}
[/code]

EDIT:
This is the error I get:

[quote]

Error: Number:-2146823281 Description:’A122_answer.length’ is null or not an object

[/quote]

Any insight about the problem is much appreciated.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@irishman101authorJun 28.2010 — I apologize for the thread title, i didn't realize it was frowned upon to ask for help in the title.
Copy linkTweet thisAlerts:
@tirnaJun 28.2010 — if you post the html for your form and radio buttons as well it will be easier to find the cause of the problem. I'm not sure why the .length would cause a problem without seeing your html.
Copy linkTweet thisAlerts:
@irishman101authorJun 28.2010 — Here is the form that contains the radio buttons (the form is the same for each set of buttons, just with a different appropriate name):
<i>
</i>&lt;form&gt;
&lt;input type="radio" name="A111_answer" value="yes" /&gt;Yes&lt;br /&gt;
&lt;input type="radio" name="A111_answer" value="no" /&gt;No&lt;br /&gt;
&lt;input type="radio" name="A111_answer" value="maybe" /&gt;Maybe&lt;br /&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@MrRedJun 28.2010 — Just quick thought - could you run to the longest .length in one "for loop" and test for each form, with suitable tests for being less than a given length? Do you use a de-bugger like the Micro$oft one? or Firebug for FF? I like Firebug - as long as you back it up with tests on IE because they both suck you in to their flavour on specific attributes/properties.

Does it read the length of the second one properly - if not using a de-bugger - I use an alert eg:alert("Form.A111_answer.length= "+Form.A111_answer.length+" - Form.A122_answer.length= "+Form.A122_answer.length);
when I clutch at straws, I CLUTCH.
Copy linkTweet thisAlerts:
@irishman101authorJun 28.2010 — Thanks for the advise, but I don't really know what your talking about.. :/

I am very inexperienced and have not been exposed to much in the javascript/programming realm. All I know is that I use NotePad to type the code and IE to test it.

I feel really stupid, but it would be much appreciated if you replied as if writing in a "Javascript for Dummies" book.:o
Copy linkTweet thisAlerts:
@tirnaJun 28.2010 — it's getting really late in the evening here, so at this stage it is quicker for me to give you the code for the way I would do it than try to debug yours.

This code will display the value of whatever radio button you click in the form. Maybe you can use this code as a guide

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

function showValue(elem) {

var elemName = elem.name
//get the radio buttons for this name
var radBtns = document.getElementsByName(elemName);

for(var i=0; i < radBtns.length; i=i+1) {
if(radBtns[i].checked) {
alert(radBtns[i].value);
}
}
}

//-->
</script>

</head>
<body>

<form>

<input type="radio" name="A111_answer" value="yes" onclick="showValue(this)" />Yes<br />
<input type="radio" name="A111_answer" value="no" onclick="showValue(this)" />No<br />
<input type="radio" name="A111_answer" value="maybe" onclick="showValue(this)" />Maybe<br />

<input type="radio" name="A222_answer" value="yes_2" onclick="showValue(this)" />Yes 2<br />
<input type="radio" name="A222_answer" value="no_2" onclick="showValue(this)" />No 2<br />
<input type="radio" name="A222_answer" value="maybe_2" onclick="showValue(this)" />Maybe 2<br />

</form>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@irishman101authorJun 28.2010 — Thank you for the help, I tried to use the code you provided, but I'm not sure how to use that for coding a submit button (rather than from clicking the radio button).
Copy linkTweet thisAlerts:
@tirnaJun 28.2010 — ok, no problem.

To display the values of the selected buttons after clicking submit, you could use something like this.

And then after doing whatever other validation you require, validateForm() would return a value of true or false depending on whether the rest of the form data is valid and so either submit or not submit the form.

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

function showValue(elemName) {
//get the radio buttons for this name
var radBtns = document.getElementsByName(elemName);

for(var i=0; i < radBtns.length; i=i+1) {
if(radBtns[i].checked) {
alert(radBtns[i].value);
}
}
}
//----------------

function validateForm() {
//display value selected of first set of radio buttons
showValue("A111_answer");
//display value selected of second set of radio buttons
showValue("A222_answer");
//the rest of your validation code could go here
}

//-->
</script>
</head>
<body>
<form action="#" method="post" onsubmit="return validateForm()">
<input type="radio" name="A111_answer" value="yes" />Yes<br />
<input type="radio" name="A111_answer" value="no" />No<br />
<input type="radio" name="A111_answer" value="maybe" />Maybe<br />

<input type="radio" name="A222_answer" value="yes_2" />Yes 2<br />
<input type="radio" name="A222_answer" value="no_2" />No 2<br />
<input type="radio" name="A222_answer" value="maybe_2" />Maybe 2<br />
<input type="submit" value="submit" />
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@MrRedJun 29.2010 — Error: Number:-2146823281 Description:'A122_answer.length' is null or not an object [/quote] is telling you that the form doesn't exist.

CHeck your spelling of everything in the form, and see how it matches with the JavaScript.

If JavaScript can't see the Form it may be because the form is spelled differently (check capitals) or there may be one mistake in the form (compare the two for similarity) and then the form doesn't behave like a form.

One of my usual sillies is to close the tag incorrectly, or overlap tags.

eg this is not cross-browser safe
&lt;SPAN name="blahblah"&gt;&lt;B&gt;Blah Blah&lt;/SPAN&gt;&lt;/B&gt;. and:

&lt;FORM&gt;....
&lt;FORM&gt;
instead of doing it properly&lt;FORM&gt;....
&lt;/FORM&gt;
(applies to any tag) can throw things.

Well that's what I would look for first. (PS I just did that to the code tags above. DOH!)
Copy linkTweet thisAlerts:
@irishman101authorJun 29.2010 — Thanks for all the help, I realized the problem was that i had nested forms within my primary form, so the objects didn't seem to exist because they were within an additional form. I removed the nested forms and now it works.?
×

Success!

Help @irishman101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...