Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 28th, 2006, 05:51 AM
Authorized User
 
Join Date: Sep 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting form element names in a loop

I've some Javascript code to iterate through the form elemenets and get their names.
I am wanting to get all the names of unique radio groups and then later test them for a value <> -99.

IF I do this
    vfrmElements = document.question.elements;
    vinNumElements = vfrmElements.length;

    vtxOutput = vfrmElements[0].name + ", " + vfrmElements[1].name + vfrmElements[7].name;

    alert ("Collected Array is " + vtxOutput);
IT WORKS FINE:
HOWEVER when I iterate through the loop of elements and access the elements with the square brackets and integer [i], it doesn't work!
I get undefined, or no properties.
I tried lots of things, but don't get what I want.
I don't get why [i] doesn't work, but [0] ... [71] will work?

    for (i=0; i<= vinNumElements; i++) {
        vfrmElement = vfrmElements[i];
        vtxOutput = vtxOutput + vfrmElement.name + ", "; // This line produces an Error.

    }
    alert ("alert Elements are :" + vtxOutput);
    alert ("Finished loadarray");




Database Agreements
__________________
Database Agreements
 
Old January 28th, 2006, 06:42 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

As you don't state the error I'm guessing that it's "... is null or not an object" or the like. You are setting the upper limit of the loop too high as the loop index, "i", should be less than the length of the array not equal to it. I would also use getElementsByName for radio buttons so the have the code as:
Code:
var colRadioGroup = document.getElementsByName("myGroup");
var sValues = "";
for (var i = 0; i < colRadioGroup.length; i++)
{
  if (i != 0) sValues += ", "; 
  sValues += colRadioGroup[i].value;
}
alert(sValues);
--

Joe (Microsoft MVP - XML)
 
Old January 28th, 2006, 12:37 PM
Authorized User
 
Join Date: Sep 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Joe,
I don't have the radio group names before hand.
I'm just testing it out as if I have the radio group namein this Javascript code to see how to write the code to find those names, i.e. proove that I can access them, then store their names in an array for later checking the value of each group.

The loop iteration is a minor problem, its fixed.

The point is document.question.element["rfs1"] works
but when I iterate through the elements it doesn't.

Must be a way to do this.
The error is merely that if I reference the name property, it says that the element has no properties. This is just within the loop.

I'm obviously doing something wrong.
What else can it be, would the id names of the <input type="radio"
elements make a difference. They are different.

Hope this makes sense.



Database Agreements
 
Old January 28th, 2006, 12:42 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What error do you get?
What does the HTML look like?

--

Joe (Microsoft MVP - XML)
 
Old January 28th, 2006, 01:09 PM
Authorized User
 
Join Date: Sep 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,
It indeed was the variable loop maximum.
This didn't make sense to me, because of previous output.
I always think when I'm writing Javascript I have to go so slowly.

Now, I want to put the element names in an array passed to this function. This function is run with the onload event.
Then the array is accessed again with the submit click.

I tried this:
varQuest[varQuest.length] = vtxRadioName;
but the error is "the array has no properties."
Can you suggest what is going on?

I'm trying the associative route now.
varQuest[vtxRadioName] = false;

Then I think I'll look at all the keys in this array in the submit event and see if the radio button is checked.

Your code document.getElementsByName["name"] should then work perfectly.

Thanks.

Database Agreements
 
Old January 30th, 2006, 12:45 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii crapanz!!
  vfrmElements = document.question.elements;
    vinNumElements = vfrmElements.length;
here vinNumElements is the total number of elements if say 6 then starting index is started from 0 based ,so your code work fine till
 vfrmElements[0], vfrmElements[1], vfrmElements[2], vfrmElements[3], vfrmElements[4], vfrmElements[5]
your code will work with
  for (i=0; i<vinNumElements; i++) {
        vfrmElement = vfrmElements[i];
        vtxOutput = vtxOutput + vfrmElement.name + ", "; // This line produces an Error.

    }

Note for (i=0; i<= vinNumElements; i++) is changed
     with --->for (i=0; i<vinNumElements; i++)

Hope this will help you





Cheers :)

vinod





Similar Threads
Thread Thread Starter Forum Replies Last Post
Element Names - Dashes Suu XML 3 August 2nd, 2007 08:49 AM
Print directory file names in a loop ypomonh XSLT 2 May 14th, 2007 05:47 AM
UserType Variables--loop thru their names? Paul Gerken VB How-To 10 December 4th, 2006 01:08 PM
Duplicate Element Names carolynk XML 1 September 30th, 2004 09:22 AM
select wildcard element names groovepapa XSLT 4 September 1st, 2004 08:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.