onChange problem

O

Omar

Hi,

Could you please tell me what am I doing wrong in the next sentence?

<select name='id' onChange="window.location='dePaso.jsp?nombre=id&valor="+this.options[this.selectedIndex].value+"'">

TIA
 
L

Lee

Omar said:
Hi,

Could you please tell me what am I doing wrong in the next sentence?

<select name='id'
onChange="window.location='dePaso.jsp?nombre=id&valor="+this.options[this.selectedIndex].value+"'">

You want (this.options[this.selectedIndex].value) to be evaluated
at the time that the onchange handler fires.

As written, it is evaluated at the time that you define the handler.
Try:

onchange="window.location=this.options[this.selectedIndex].value"
 
R

RobB

Omar said:
Hi,

Could you please tell me what am I doing wrong in the next sentence?

<select name='id' onChange="window.location='dePaso.jsp?nombre=id&valor="+this.options[this.selectedIndex].value+"'">

TIA

Those double-quotes bounding the onchange handler string go around the
*entire* string; the singles are the ones you need to delimit the
literal part of the url (and discontinue, to insert a variable):

<select name="id"
onChange="window.location='dePaso.jsp?nombre=id&valor='+this.value">

Select.value is pretty ubiquitous these days, someone will correct me
if that's inaccurate.

Might want to hook up a button to do this, users are notorious for
fumbling listboxes...
 
D

DU

Omar said:
Hi,

Could you please tell me what am I doing wrong in the next sentence?

<select name='id' onChange="window.location='dePaso.jsp?nombre=id&valor="+this.options[this.selectedIndex].value+"'">

TIA

1- name="id" is a very bad choice of name value. And id="name" is also a
bad choice of id value. name="name" and id="id" are also bad choices.
Using the same string to identify both the name and id attributes is
also a bad coding practice.

2- In your code, it is more robust to use
window.location.href =

3- I think you are not using quotes accordingly. How about:

onchange = "window.location.href = 'dePaso.jsp?nombre=id&amp;valor=' +
this.options[this.selectedIndex].value;">

4- You also need to escape "&" in the href/query string to avoid
validation problems and make sure you're not referring to a named
character entity reference.

So, I would recommend:

<select onchange = "window.location.href =
'dePaso.jsp?nombre=id&amp;valor=' +
this.options[this.selectedIndex].value;">

if id is related to the select, then you need to adjust your code
accordingly with this.id in the query string.

DU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top