www.webdeveloper.com
Results 1 to 3 of 3

Thread: Select Onchange problem.

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Post Select Onchange problem.

    I have a select statement that redirects to a different page when it is created.
    I know this is not the best way of doing forms but this is the way I have to do it.
    I am having problems adding the selected item from the select box to add it to the url.

    This is the code I have:
    HTML Code:
    <select onChange="window.location.href='stateSearch.jsp?stateId=this.options[this.selectedIndex].value'" name="StateId" >
      <option value="3">State 1</option>
      <option value="2">State 2</option>
      <option value="1">State 3</option>
    </select>
    As is now, the onchange part works and i get redirected; but the value for stateId in stateSearch.jsp is literally "this.options[this.selectedIndex].value"
    as opposed to be the actual selected value.

    Any Ideas on how I can fix this?

  2. #2
    Join Date
    Aug 2009
    Posts
    593
    Move the select elements value out of the string...

    HTML Code:
    <select onchange="window.location.href='stateSearch.jsp?stateId=' + this.options[this.selectedIndex].value;" name="StateId" >
    	<option value="3">State 1</option>
    	<option value="2">State 2</option>
    	<option value="1">State 3</option>
    </select>
    Last edited by thraddash; 11-24-2010 at 02:53 PM.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    If not a multiple, the select element takes, automatically, the value of its selected option. Simplify as:
    Code:
    onchange="location.href='stateSearch.jsp?stateId=' + this.value;"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles