4

i'm writing some inline javascript. it's not working and i'm not sure why.

at the top of the index page

<meta http-equiv="Content-Script-Type" content="text/javascript" />

later on i have:

<select name='id'> 
<option value=-1>New Entry</option> 
<option value='1' onclick="location.replace('index.php?page=update&id=1')">2010-06-12 16:38:08</option> 
<option value='2' onclick="location.replace('index.php?page=update&id=2')">2010-06-12 18:20:49</option> 
<option value='3' onclick="location.replace('index.php?page=update&id=3')">2010-06-13 11:39:09</option> 
</select>

what i want is for the page to be replaced when one of the option items is selected but the code is not causing a page refresh and i'm not sure why. is something wrong with the javascript?

2
  • That seems to work for me in Firefox. I presume you are testing in IE? I'm not sure that supports option.onclick based on here bytes.com/topic/javascript/answers/… Commented Jun 19, 2010 at 18:09
  • was using chrome. this does work in firefox... didn't realize. thanks for the tip. Commented Jun 19, 2010 at 18:15

2 Answers 2

4

Use the onchange event of the select element:

<select name="id" onchange="window.location.replace('index.php?page=update&id='+this.options[this.selectedIndex].value);"> 
<option value="-1">New Entry</option> 
<option value="1">2010-06-12 16:38:08</option> 
<option value="2">2010-06-12 18:20:49</option> 
<option value="3">2010-06-13 11:39:09</option> 
</select>

Note: The location.replace method is used when you want to navigate to the page and also replace the current page in the browsing history. If you just want to navigate to the page normally, you assign the URL to the window.location.href property instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@digitalFresh: The value property doesn't work in older browsers. I haven't checked recently how old the browser has to be not to support it, but reading the value from the options collection always works.
2

I've found that window.location.href doesn't work at all in newer browsers (FF4, Chrome 10, etc.).

1 Comment

If using window.location from a <input type="submit", use instead <input type="button".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.