Changing An IFrame Source From A Drop-Down List Via JavaScript / DOM
Recently asked on Yahoo! Answers:
I am looking for a drop down box that links to a iframe. . .?
the same drop down box that goes with a form that changes based on what is in the drop down box. HELP Please!!!
This is a very easy task to accomplish, provided you know the basics of Document Object Model (DOM) manipulation and JavaScript. As always, I’ll have a link to a working demo and downloadable code at the end of this entry.
Step 1: Create The XHTML Form
To begin, we need some simple HTML elements:
- A basic form;
- A drop-down (select) list that will contain the URLs we want to use;
- An iFrame that will display the page we select from the drop-down list.
Note that we add the name of our JavaScript function, setIframeSource(), to the onchange event for the select (drop-down) list.
<form id="form1" method="post"> <label> Select A Location: <select id="location" onchange="setIframeSource()"> <option value="dummy.html">Select a location ...</option> <option value="http://www.yahoo.com">Yahoo!</option> <option value="http://answers.yahoo.com">Yahoo! Answers</option> <option value="http://www.mozilla.org">Mozilla.org</option> <option value="http://www.dunkindonuts.com">Dunkin Donuts</option> </select></label> </form> <iframe id="myIframe" src="dummy.html" frameborder="0" marginwidth="0" marginheight="0" width="800" height="400"></iframe>
Step 2: The JavaScript Function
The JavaScript function that powers our IFrame is simple as pie:
- We set variables to represent the select list and the IFrame in our document.
- We set the value of a variable we called theUrl to be the value of the currently selected option from the drop-down list.
- We set the source of the IFrame to be theUrl.
function setIframeSource() {
var theSelect = document.getElementById('location');
var theIframe = document.getElementById('myIframe');
var theUrl;
theUrl = theSelect.options[theSelect.selectedIndex].value;
theIframe.src = theUrl;
}
Every time we change the value in the drop-down list, the IFrame’s source will also be changed. It’s that simple.
You can see a working demo of this script here:
http://www.dougv.com/demo/js_ddl_iframe/
Just save the page to your computer and tinker with it to suit your needs. I distribute code under the GNU GPL version 3.
Related Posts
- Changing CSS Styles Via JavaScript, Based On The User's Web Browser (19.7)
- Dynamically Populating A Listbox From A Textbox Via JavaScript / DOM (18.8)
- Showing A Larger Image From A Thumbnail OnClick Via JavaScript / DOM (18.8)
- Retaining Selections From A Single Select Box In Two Multiple Select Boxes Via JavaScript / DOM (18.7)
- Fun With JavaScript: A Simple Test / Quiz Script To Demonstrate DOM Form Handling (18.1)
The numbers inside parentheses are relevance scores. Scoring is based, in order of priority, on title, category, content and tags. The higher the score, the more likely that post relates to this post.


[...] AJAX has been used (stupidly) for site navigation since well before this guy thought of it; and it has been used (stupidly) to obfuscate site content for just as long. Hell, I’ve discussed variations on these simple concepts here, here and here. [...]
Thanks for this – helped me out a lot.
How would you change from a dropdown select to a onclick button action on an array?
Dave: It should work fundamentally the same. I will explain it but there are a number of other topics / posts ahead of you in the queue, so it will likely be a while before I get to it. If you would like to make a purchase from my Amazon.com wish list (linked under “Need More Help Or Want To Say Thanks?”, above), I will be happy to move you to the top of the queue. Thanks!