SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Jul 22, 2003, 22:49 #1
- Join Date
- Jan 2002
- Location
- Strongsville OH
- Posts
- 1,534
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
accessing fieldnames with [] in them
Is there a way in Javascript to access a field with [] in it for example
destList[]. I need to have this since the field is a listbox and it contains multiple options. it gets posted to the server and handled by PHP. in order to have all the options submitted to the server the name of the field needs to have a [] on the end of it.
-
Jul 22, 2003, 23:33 #2
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use the elements collection
var myRef = document.forms['formName'].elements['elementName'];
I recommend always using collections instead of the exposed properties.
-
Jul 23, 2003, 07:58 #3
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
<script type="text/javascript">
<!--
function doit(var1, var2)
{
switch(var1)
{
case 1:
alert(document.a[var2].value)
break;
case 2:
var fldName = var2.name;
alert(document.a[fldName].value);
break;
case 3:
alert(document.a[var2].value)
}
}
//-->
</script>
<form name='a'>
<input type="text" name="b[0]" onBlur='doit(1, this.name)' value="0">
<input type="text" name="b[1]" onBlur='doit(2, this)' value="1">
<input type="text" name="b[2]" onBlur="doit(3, 'b[2]')" value="2">
</form>
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Jul 23, 2003, 08:12 #4
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Interesting vincent, but there's no difference between 1 and 3, so you really only have two cases, and I'd do it this way
<script type="text/javascript">
<!--
function doit(var1)
{
switch(typeof var1)
{
case 'string':
alert(document.a.elements[var1].value)
break;
case 'object':
alert(var1.value);
break;
}
}
//-->
</script>
<form name='a'>
<input type="text" name="b[0]" onBlur='doit(this.name)' value="0">
<input type="text" name="b[1]" onBlur='doit(this)' value="1">
<input type="text" name="b[2]" onBlur="doit('b[2]')" value="2">
</form>
Of course - it's a little silly to use a switch statement for only two possibilities
-
Jul 23, 2003, 11:14 #5
- Join Date
- Feb 2000
- Location
- where the World once stood
- Posts
- 700
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Beetle,
interesting use of 'typeof'
I called it 3 to point out the use of 'this.name' and 'b[0]'
VinnyWhere the World Once Stood
the blades of grass
cut me still
-
Jul 23, 2003, 11:48 #6
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Vincent Puglia
<input type="text" name="b[0]" onBlur='doit(1, this.name)' value="0">
<input type="text" name="b[1]" onBlur='doit(2, this)' value="1">
<input type="text" name="b[2]" onBlur="doit(1, 'b[2]')" value="2">
And leave "case 3" off, because it's pure repitition.
Bookmarks