User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 401,504 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,318 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1495 | Replies: 11 | Solved
Reply
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Help Array Names

  #1  
Jan 27th, 2008
Hi Everyone

In a html document I declare several input tags.

They all have the attribute name="input[]".

So when they are intialized as input[0], input[1], etc.

Now after they have been intialized I want to be able to access them using Javascript.

How would I do this?

The usual code in javascript is not working:

form.field.value.attribute

Thanks, Regards X
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Array Names

  #2  
Jan 27th, 2008
If you want to access the value of input element "input[n]" try document.forms[formName].elements["input[n]"].value

If you want the names of all the input elements, try:
function getInputNames(frm) {
  var arr = [];
  var elms = frm.getElementsByTagName("input");
  for(var i = 0, max = elms.length; i < max; ++i) {
    arr[i] = elms[i].name;
  }
  return(arr);
}
Read the Javascript FAQ for more details.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Array Names

  #3  
Jan 27th, 2008
Ya I researched the first line but that wasnt working for me: eg.

document.forms[form_input].elements["input[0]"].value.length == 0

Thats what I am using there any problems?

Thanks, Regards X

PS: Thanks for the names function ill keep that in mind if i need it, atm just need to refer to one of the elements in the array.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Array Names

  #4  
Jan 27th, 2008
Is form_input the variable holding the form name or is it the form name? If it's the form name, you need to enclose it in double quotes. If it still doens't work, test the page in Firefox, look at the error console and let me know what it says.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2007
Posts: 75
Reputation: hielo is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
hielo hielo is offline Offline
Junior Poster in Training

Re: Array Names

  #5  
Jan 27th, 2008
If your fields have names such as "input[]", you do not need to provide any numbers inside the brackets. That naming convention (adding brackets at the end of the field name, is necessary for PHP processing, but on the client-side, those brackets are part of the name). Refer to the full example below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</style>
<script language="JavaScript" type="text/JavaScript">
function toggle(status){ 
	var chk = document.getElementsByName("ad_pages[]");
	
	for(var i=0,limit=chk.length; i < limit; ++i)
	{
		chk[i].checked = !chk[i].checked;
	}
 
} 
 
 
</script>
</head>
 
<body> <form>
            <table border="0" align="center" cellpadding="3" cellspacing="3" bgcolor="#F3F3F3">
                         
                         <tr>                            
                              <td width="10"><label><input name="ad_pages[]" type="checkbox" id="ad_pages[]" value="1"  ></label></td>
                              <td width="180"> English</td>
                              <td width="10"><label><input name="ad_pages[]" type="checkbox" id="ad_pages[]" value="2"  ></label></td>
                              <td width="180">Maths</td>
                              <td width="10"><label><input name="ad_pages[]" type="checkbox" id="ad_pages[]" value="3"  ></label></td>
                              <td width="180">Science</td>
                              <td width="10"><label><input name="ad_pages[]" type="checkbox" id="ad_pages[]" value="4"  ></label></td>
                              <td width="180">Technology</td>
          </tr>   
                  </table>                          
             <table width="500" border="0" align="center" cellpadding="5" cellspacing="5">
                              <tr>
                                <td><div align="center">
                                  <input name="toggleAll" type=button class="formbutton" onClick="toggle()" value="Toggle All">
                                </div></td>
                              </tr>
</table></form>
</body>
</html>
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Array Names

  #6  
Feb 2nd, 2008
Sorry about the late reply but had commitments at work but still no luck getting this working.

I try running it and it just bypasses the javascript syntax.

I tried both solutions and neither worked.

document.forms["form_input"].elements["input[0]"].value.length == 0

Maybe If i simplify it down:
1. I have multiply input file boxes with the same name
2. I need to ONLY refer to the first input file
3. I need to check if that input file has a file selected in it (aka TEXT aka LENGTH)

4. Help me please =(

Thanks, Regards X
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Array Names

  #7  
Feb 3rd, 2008
I am confused here, maybe you should paste the markup (HTML) next time so we have a clear view of what we are dealing with here.

Do you have a markup like this?
<input name="input[]">
<input name="input[]">

or like this?
<input name="input[0]">
<input name="input[1]">

If it's the latter, then what I have posted should work. You get hold of the first input element using document.forms[0].elements["input[0]"]. But if it's the former, there is no unique way of accessing the input element. That is possible only if you have an element with a unique id or a unique name. In that case, you do something like:
var els = document.getElementsByName("input[]"); // See this
for(var i = 0, max = els.length; i < max; ++i) {
  /* access the input elements using the index i. */
}
Last edited by ~s.o.s~ : Feb 3rd, 2008 at 12:14 am.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Array Names

  #8  
Feb 3rd, 2008
latter = after?
former = previous?
Going on that assumption I presume.

The HTML mark up I am using is like the one you refered to as "former":
<input name="input[]">
<input name="input[]">

So I assuming the below code should work?
Is this correct? as I plan to try it once I am at home.

var els = document.getElementsByName("input[]"); // See this
for(var i = 0, max = els.length; i < max; ++i) {
 /* access the input elements using the index i. */
 /* The code I would have here is ? */
 els[i].value.length = 0;
}

Thanks for all the help, much appericated.

Regards, X
Last edited by OmniX : Feb 3rd, 2008 at 5:15 pm.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Array Names

  #9  
Feb 4th, 2008
Looks good except for the thing that you don't explicitly set the length of the string since it's a read only property and would have no effect. Instead set the value of the form element to an empty string. Something like els[i].value = '';
Last edited by ~s.o.s~ : Feb 4th, 2008 at 10:36 am.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2007
Posts: 352
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Array Names

  #10  
Feb 4th, 2008
Didnt get home till late last night, didnt get a chance to test it hopefully I get time to tonight.

The code I will be testing is:
var els = document.getElementsByName("input[]"); // See this
for(var i = 0, max = els.length; i < max; ++i) {
 /* access the input elements using the index i. */
 /* The code I would have here is ? */
 els[i].value = "";
}

Thanks for all the help SOS, ill check it tonight and keep you informed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 2:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC