Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Looping through form objects with the same name in JS

Posted on 2011-02-16
5
Medium Priority
?
183 Views
Last Modified: 2012-05-11
I am using JS for validating user input.  The amount of lineitems that appear depend on the # of recors in the DB.  If I have more than 1 record my code works becuae I loop through the name of that textbox and i (in brackets of course).  ]The problem is when there is one record-only one text box.  Js doesn't create an array so textboxname 0 is null.  I assumed that if there is one textbox it would have 0as its index.  How can  I code for this scenario?  thanks
0
Comment
Question by:JSCF
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Justin Mathews earned 1000 total points
ID: 34909127
Use the loop only if textbox.length is defined else use textbox.value to get the only element's value.

if (document.forms[0].textbox.length)
//loop
else
val = document.forms[0].textbox.value;
0
 

Author Comment

by:JSCF
ID: 34909182
OK, but I was wondering about this because I have another page where users can add records.  If there is an error in user input.  It returns that one record-I loop through it in a very similar way and I do not get an error that time even though there is one textbox with that name on the page-wht is that?
0
 
LVL 16

Expert Comment

by:Justin Mathews
ID: 34909500
I can't say why it is happening. Generally the rule of the thumb is if you have multiple elements with the same name you get a collection otherwise you get a single element.
0
 

Author Comment

by:JSCF
ID: 34909662
Is this common practice to allow for these two scenarios and basically repeat the code twice-when validating with JS?
0
 
LVL 16

Assisted Solution

by:Justin Mathews
Justin Mathews earned 1000 total points
ID: 34909891
Yes, it is safest to do so. Or you should think of giving different names for the text boxes like textbox1, textbox2, etc.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

671 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question