Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 20th Oct 2012, 07:47 PM   #1
100% Warrior
War Room Member
 
webpeon's Avatar
 
Join Date: 2012
Location: New Zealand
Posts: 588
Thanks: 90
Thanked 76 Times in 71 Posts
Default
Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

I have a page/form which I need to iframe into my website for branding purposes.

The page/form I want inside the iframe has a dropdown menu which I have no control over.. but need to have that dropdown pre-select a particular menu item when it loads inside my iframe..

How do I do this?

Web 2 Mobile
The Future of The Web
webpeon is offline  
Unread 22nd Oct 2012, 04:05 AM   #2
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

here what you do:

Warning: this works only on server side, otherwise you will see security errors and warnings.


file1

<html>
<body>

<!-- your iframe, see file2 below -->
<iframe src="frame.html" frameborder="0" width="500" height="300" id="iiframe"></iframe>

<!-- get latest jquery library from google apis -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<!-- jquery code below find in iframe with ID:iiframe, the element with ID:selcolor and option attribute value="green" and marks it as selected -->
<script type="text/javascript">
(function(){
$('#iiframe').contents().find('#selcolor option[value="green"]').attr('selected', 'selected');
})();
</script>
</body>
</html>


file2

<html>
<body>
<select name="selcolor" id="selcolor">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</body>
</html>
viescripts is offline  
The Following User Says Thank You to viescripts For This Useful Post:
Unread 22nd Oct 2012, 04:57 AM   #3
100% Warrior
War Room Member
 
webpeon's Avatar
 
Join Date: 2012
Location: New Zealand
Posts: 588
Thanks: 90
Thanked 76 Times in 71 Posts
Default
Re: Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

Thanks vie.. i'll have a play with it tomorrow.

Web 2 Mobile
The Future of The Web
webpeon is offline  
Unread 22nd Oct 2012, 11:21 AM   #4
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

Originally Posted by webpeon View Post

Thanks vie.. i'll have a play with it tomorrow.
forgot, you need to name the file2: frame.html or change the file name in iframe.
I can drop a link to a working example if necessary.
viescripts is offline  
Unread 24th Oct 2012, 04:46 AM   #5
Web Developers
 
Sitesupplier's Avatar
 
Join Date: 2012
Posts: 22
Thanks: 1
Thanked 5 Times in 5 Posts
Default
Re: Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

Originally Posted by webpeon View Post

I have a page/form which I need to iframe into my website for branding purposes.

The page/form I want inside the iframe has a dropdown menu which I have no control over.. but need to have that dropdown pre-select a particular menu item when it loads inside my iframe..

How do I do this?
When you say you have no control over it, does this mean no access to the source? If this is the case, can you not simply e-mail the necessary person to take care of this? You don't need to use JavaScript to have a given item pre-selected, you just do this:

Code:
<select id="dropdown_options">
    <option id="one">Option one</option>
    <option id="two" selected>Option two</option>
    <option id="three"> Option three</option>
</select>
It's also completely unnecessary to need the jQuery library to achieve this. Some basic JS is all you need:

Code:
<select id="dropdown_options">
    <option id="one">Option one</option>
    <option id="two">Option two</option>
    <option id="three"> Option three</option>
</select>

<script type="text/javascript">
(function()
{
    document.getElementById("dropdown_options").options["two"].selected = true;
})();
</script>
http://jsfiddle.net/FZGbC/2/

Note: just to be clear, you can access the iframe source in JavaScript like so:

Code:
<iframe name="test" id="test"></iframe>

<script type="text/javascript">
    console.log(test.document);
</script>

Sitesupplier is offline  
Unread 29th Oct 2012, 01:16 AM   #6
Active Warrior
 
Join Date: 2012
Location: Edmonton AB, Canada
Posts: 52
Thanks: 0
Thanked 3 Times in 3 Posts
Default
Re: Can I select a dropdown value in an iframe?
Share on: 
fb share twitter share gplus share more share

This is great, thanks!

Discover How To Quit Your Job And Work Online In My Free Video Course!
http://mikestankowski.com/quityourjob
Mikestankowski is offline  
Closed Thread


Bookmarks

Tags
dropdown, iframe, select


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 02:34 PM.