Jump to content

Dropdown option - onclick


karimali831

Recommended Posts

Hi,

 

When I use

<option value="http://link.com" onclick="return confirm(\'This will take you to this link\');">Link</option>

 

When I select this from the dropdown "Link", it will direct me straight to the link despite the onclick return showing.

So I select it from dropdown, shows the onclick but directs me to the page before I have a chance to click on OK or CANCEL?

Link to comment
Share on other sites

Change the event from "onclick" on each option to "onchange" in the select tag. Then use "this.value" to determine the selected value. Also, this is JavaScript, not PHP, so it really should have been posted in the JS board, and you should move this to a function to have better control over it.

Link to comment
Share on other sites

Not on the <option> tag, on the <select> tag.

JS:

function goToSite(site){
if (confirm("Are you sure you want to go to "+site+"?")){
	window.location = site;
}	
}

HTML:

<select onchange="goToSite(this.value);">
<option value="http://google.com/">Google</option>
<option value="http://yahoo.com/">Yahoo</option>
</select>

Link to comment
Share on other sites

JS:

function goToSite(site){
if (site.value.length > 0){
	if (confirm("Are you sure you want to go to "+site+"?")){
		window.location = site.value;
	} else{
		site.value = "";
	}
}
}

HTML:

<select onchange="goToSite(this);">
<option value="">Select One</option>
<option value="http://google.com/">Google</option>
<option value="http://yahoo.com/">Yahoo</option>
</select>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.