chowmouse Posted September 28, 2010 Share Posted September 28, 2010 Hey guysI am quite a novice when it comes to JS and HTMLWhat i need to do is have a HTML document which displays a drop down menu. When something in the drop down menu is selecteda user then click go, and then a new page opens. (i want each of the selections in the drop down menu to open a new window to a different website)I would like to use the onclick event and the Window.Open Method from the W3Schools website.Can anyone please helpThanks in advanceRegards Link to comment Share on other sites More sharing options...
chowmouse Posted September 28, 2010 Author Share Posted September 28, 2010 Hey guys. I have this code which contains a drop down menu as well as a "go" button. When the go button is clicked the page is directed to the relevant URL in the drop down code.I need to have this "go" button open the URL's in a new window. I want to use the window.open method, but i'm not sure how to code it in this context.Can someone please help.thanks very much in advance Link to comment Share on other sites More sharing options...
trevelluk Posted September 28, 2010 Share Posted September 28, 2010 *Edit* - sorry, clicked the wrong button for my first post.If I understand it right, you have a select field in which each option has a URL as its value. So something like: <select><option value="http://www.google.com/">Google</option><option value="http://www.w3schools.com">W3Schools</option></select> And then somewhere else in the page you have a Go button (is this actually a button, or a link? Not that it really matters for what I'm about to show you).I'd suggest adding an id to the select field if it doesn't already have one, so something like <select id="urls">...</select>Then write yourself a little JavaScript function to open the window - in the document head: function openWindow(){ // Get a reference to the select field. var select = document.getElementById('urls'); // Get the value of the selected item. var url = select.options[select.selectedIndex].value; // Open the new window. window.open(url);} And set the onclick attribute of the button (or link, or whatever) to call the function, something like this: <!-- In your document somewhere --><button onclick="openWindow()">Go</button> Link to comment Share on other sites More sharing options...
aspnetguy Posted September 28, 2010 Share Posted September 28, 2010 <select name="options" id="options"><option value="http://www.google.com">Google</option><option value="http://www.yahoo.com">Yahoo</option><option value="http://www.bing.com">Bing</option>'</select><input type="button" value="Go" id="gobutton" /><script type="text/javascript"> var button = document.getElementById("gobutton"); var select = document.getElementById("options"); button.onclick = function() { window.open(select.options[select.selectedIndex].value); };</script> Link to comment Share on other sites More sharing options...
aspnetguy Posted September 28, 2010 Share Posted September 28, 2010 Is there a reason you posted this question again after only 15 minutes of posting it the first time? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.