SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [solved] Problem with square brackets in variable name

    I have the following code that works just fine:

    Code:
      <html>
      <body>
      <SCRIPT LANGUAGE="JavaScript">
      <!-- Begin
      function change() {
      	document.myform.mytext.value = document.myform.myselect.options[document.myform.myselect.selectedIndex].text;	
      }
      //  End -->
      </script>
      	<form name="myform">
      		<select name="myselect" onchange="change();">
      			<option selected value="0" onselect="">-- pick something --</option>
      			<option value="1">You chose 1</option>
      			<option value="2">You chose 2</option>
      			<option value="3">You chose 3</option>
      		</select>
      		<input type="text" name="mytext" value="" />
         </form>
      </body>
      </html>
    This will insert "You chose 1" into the textfield when this choice is made in the dropdown. So far so good!

    Now, to the problem:

    How can I make the javascriptfunction work in the same manner when the variable names contain square brackets? Like this:
    Code:
      	<form name="myform">
      		<select name="myselect[example]" onchange="change();">
      			<option selected value="0" onselect="">-- velg noe --</option>
      			<option value="1">Du valgte 1</option>
      			<option value="2">Du valgte 2</option>
      			<option value="3">Du valgte 3</option>
      		</select>
      		<input type="text" name="mytext[example]" value="" />
         </form>
    This javascript won't work:
    Code:
      function change() {
      	document.myform.mytext[example].value = document.myform.myselect[example].options[document.myform.myselect[example].selectedIndex].text;	
      }
    My cut and paste javascript skills have reached their limit... Anyone care to help me out?
    Last edited by torkil; Apr 7, 2006 at 06:42.
    Torkil Johnsen
    kampforum.no

  2. #2
    d^_^b Iain G's Avatar
    Join Date
    Nov 2005
    Location
    Gloucester, UK
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Square brackets usually contain index information for arrays, so myArray[1] tells the script to refer specifically to the second item in the array. For this reason you can't use square brackets as part of a variable name.
    Coding and design philosophy:
    Keep It Simple, Stoopid

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the notation

    object["parameter"] as an alternative to

    object.paramater

    So in your above example, the following will work:

    Code:
    function change() {
    
    document.myform["mytext[example]"].value = document.myform["myselect[example]"].options[document.myform["myselect[example]"].selectedIndex].text;
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the input! I appreciate it!

    I got this from a friend in a chat, and it also works like a charm:

    Code:
    <html>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function change( elem ) {
    	document.getElementById( 'mytext[example]' ).value = elem.options[ elem.selectedIndex ].text;
    }
    //  End -->
    </script>
    	<form name="myform">
    		<select id="myselect[example]" onchange="change(this);">
    			<option selected value="0" onselect="">-- pick something --</option>
    			<option value="1">You chose 1</option>
    			<option value="2">You chose 2</option>
    			<option value="3">You chose 3</option>
    		</select>
    		<input type="text" id="mytext[example]" value="" />
       </form>
    </body>
    </html>
    Torkil Johnsen
    kampforum.no


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
  •