Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > Anyone know why this javascript doesn't work?

Reply
Thread Tools

Anyone know why this javascript doesn't work?

 
 
Ed Jay
Guest
Posts: n/a
 
      12-16-2005
When I run the below js, I get the error:
"document.form1.elements[menu_name+(i)].checked) is null or not an object"

<script type="text/javascript">

function get_menu_value(menu_name) {

var mValue = 0;

for(var i=0; i < menu_name.length; i++) {

if (document.form1.elements[menu_name+(i)].checked) {

mValue = mValue + document.form1.elements[menu_name+(i)].value;
}
}
alert(mValue);
return false;
}
</script>

<form name = "form1">
Choice (11)<input type=checkbox name=choice value=11><br>
Choice (13)<input type=checkbox name=choice value=13><br>
Choice (15)<input type=checkbox name=choice value=15><br>
Choice (19)<input type=checkbox name=choice value=19><br>
Choice (100)<input type=checkbox name=choice value=100><br>
<input type = "submit" onclick="get_menu_value('choice')">
</form>

--
Ed Jay (remove M to respond by email)
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      12-16-2005
On 16/12/2005 18:27, Ed Jay wrote:

[snip]

> function get_menu_value(menu_name) {
>
> var mValue = 0;
>
> for(var i=0; i < menu_name.length; i++) {


Why does the length of the name determine the number of iterations of
this loop?

> if (document.form1.elements[menu_name+(i)].checked) {


The expression in the square brackets will be evaluated, converted to a
string, and used as a property name. In your example, these names will
be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
checkboxes (you create six names), none of these names match a control
within the form, resulting in an undefined value, and an error with the
subsequent property access.

> mValue = mValue + document.form1.elements[menu_name+(i)].value;


The value property is a string. The addition operator, with a string and
a number operand, will perform a concatenation. I suspect that you want
arithmetic addition.

[snip]

> return false;


[snip]

> <input type = "submit" onclick="get_menu_value('choice')">


As you do not return the value returned by the get_menu_value function,
it will have no effect. In any case, it is generally better to use the
submit event of the form, passing a reference to the form, rather than
the click event of the button.

function getMenuValue(menuName, form) {
var value = 0,
menu = form.elements[menuName];

for(var i = 0, n = menu.length; i < n; ++i) {
if(menu[i].checked) {
/* Unary plus performs explicit conversion to Number. */
value += +menu[i].value;
}
}
alert(value);

return false;
}

<form action="..." onsubmit="return getMenuValue('choice', this);">
<!-- ... -->
</form>

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
 
Reply With Quote
 
 
 
 
Ed Jay
Guest
Posts: n/a
 
      12-16-2005
Michael Winter <(E-Mail Removed)> wrote:

>On 16/12/2005 18:27, Ed Jay wrote:
>
>[snip]
>
>> function get_menu_value(menu_name) {
>>
>> var mValue = 0;
>>
>> for(var i=0; i < menu_name.length; i++) {

>
>Why does the length of the name determine the number of iterations of
>this loop?
>
>> if (document.form1.elements[menu_name+(i)].checked) {

>
>The expression in the square brackets will be evaluated, converted to a
>string, and used as a property name. In your example, these names will
>be 'choice0', 'choice1', ..., 'choice5'. Not only are there only five
>checkboxes (you create six names), none of these names match a control
>within the form, resulting in an undefined value, and an error with the
>subsequent property access.
>
>> mValue = mValue + document.form1.elements[menu_name+(i)].value;

>
>The value property is a string. The addition operator, with a string and
>a number operand, will perform a concatenation. I suspect that you want
>arithmetic addition.
>
>[snip]
>
>> return false;

>
>[snip]
>
>> <input type = "submit" onclick="get_menu_value('choice')">

>
>As you do not return the value returned by the get_menu_value function,
>it will have no effect. In any case, it is generally better to use the
>submit event of the form, passing a reference to the form, rather than
>the click event of the button.
>
> function getMenuValue(menuName, form) {
> var value = 0,
> menu = form.elements[menuName];
>
> for(var i = 0, n = menu.length; i < n; ++i) {
> if(menu[i].checked) {
> /* Unary plus performs explicit conversion to Number. */
> value += +menu[i].value;
> }
> }
> alert(value);
>
> return false;
> }
>
> <form action="..." onsubmit="return getMenuValue('choice', this);">
> <!-- ... -->
> </form>
>
>Hope that helps,
>

It sure does, Mike. Thank you very much. I'm using your solution for menus
where all checkboxes have the same name. (I'm going to use Evertjan's for
those menus where the names are different, e.g., choice1, choice2...)

--
Ed Jay (remove M to respond by email)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
LOOK! i just want to know does anyone know... Simon Egginton Javascript 3 07-26-2004 08:11 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
Anyone know anyone in Toronto Canada that will write your Mcsa Exams MonkeyMunx Microsoft Certification 17 01-05-2004 03:37 PM



Advertisments