SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    .NET inside archigamer's Avatar
    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.
    Web Finesse Studios
    Professional, business oriented web hosting and development.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    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.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    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>


    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    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
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    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]'

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vincent Puglia
    I called it 3 to point out the use of 'this.name' and 'b[0]'
    True, but the code in the switch for 1 an 3 are identical, so it would bet best to just illustrate the difference in how you can send the name

    <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.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •