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
https://forums.phpfreaks.com/topic/204774-dropdown-option-onclick/
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.

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.