Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript How To Change An Iframes Src On Click Reply To: How To Change An Iframes Src On Click

#169523
dyr
Participant

Here’s a simple example: http://codepen.io/shawkdsn/pen/HCfxi

Instead of buttons I opted for a select. If you need to use buttons then your javascript will differ slightly.

There’s one <select> element and (in this simplified example) two <option> elements. Each option elements’ value is the URL you want to load into the <iframe>. (you could instead store the list of URLs in an array within your javascript, but the way I’ve implemented it would let you build your HTML dynamically and not have to independently set up an array with your URLs.

The function switchIframeSrc simply sets the src attribute of the iframe to the selected option’s value (the URL.)

I read through your other thread and I believe there’s a better solution to your problem, but I would need more information to determine exactly what that is. Forms can be tricky.