Link to home
Start Free TrialLog in
Avatar of Tammu
Tammu

asked on

Set value of hidden input inside a form inside a iframe from the main page on same domain

I have a page called customer-details where there is a input field whose value is the customer name. Its NOT in a form. here is the input

<input id="custname" name="custname" type="hidden" value="John Doe" />

Open in new window


and now on the same page I have iframe which contain a form. This form contains a hidden input called customer-name whose value needs to the value of custname. The iframe has  id called "iFrame1"

and the form

<form class="form-cust" id="formContainer" action="/test.php" enctype="multipart/form-data" method="POST" novalidate="novalidate" data-form="manual_iframe">
   <div class="item-alignment-left" id="submit-button-div">
    <input class="button-special" id="submit-button" type="submit" data-regular="" value="Submit" />
  </div>
  <input name="customer-name" id="customer-name" type="hidden" value="default value" />
</form>

Open in new window


What's the most effect way to achieve this. Any example or reference is appreciated

Thanks
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Have a look at this - it'll need to go in the main page - not the iFrame page:

$(document).ready(function() {
    $('#iFrame1').contents().find('#customer-name').val( $('#custname').val() );
});

Open in new window

Avatar of Tammu
Tammu

ASKER

Thanks for reply but does not work. its not setting the value.
OK. Not really enough info there to be helpful. If you have a link to a page where we can see this in action, I'm sure we can figure out what's wrong.

Have you added the jQuery library to your document?
Avatar of Tammu

ASKER

Thanks again, can I send a private message with my site info.
Yeah sure.
Avatar of Tammu

ASKER

Thanks PM sent
Right - the hidden input in the iFrame doesn't have an ID - it just has a name. You need to add the ID that matches your jQuery, such as:

<input type="hidden" name="customer-name" id="customer-name">

Open in new window


You're also getting an error about the $ not being defined - this can happen if you've re-assigned the standard jQuery $ to something else. If that's the case, then you'll need to change the jQuery to use whatever you've assigned it to.
Avatar of Tammu

ASKER

Thanks for looking into to it. I added the id for the hidden input customer-name but I am not sure how to find the jQuery $ assigned
Probably easiest to just use the full named jQuery reference rather than the shorthand $.

Change your code to the following:

jQuery(document).ready(function($) {
    $('#fb_iframe').contents().find('#customer-name').val( $('#custname').val() );
})

Open in new window

Avatar of Tammu

ASKER

nope that help. I checked in chrome and it says now jQuery is undefined. Appreciate it
Avatar of Tammu

ASKER

I got the undefined error fixed but if you upload a image and hit submit, you will see that the customer name is not getting passed Thanks again
Worked fine for me:

User generated image
User generated image
Avatar of Tammu

ASKER

Hmm strange its not working me. I tried it in firefox and chrome. What am I doing wrong :( here my results

User generated image
User generated image
Not sure. I would make sure you've cleared down your cache and check there are no errors showing up in the console.
Avatar of Tammu

ASKER

hmmm very strange I even tried in 2 different computers and same result :(
Yeah - I've just tried it on a different computer and it didn't work. Not sure if your document.ready block is firing or not.

One possibility is that the document.ready block is being fired when the main document is ready, but before the iFrame content is ready - this would mean that the input inside the iFrame isn't available when the jQuery code fires.

As a test, add an simple console.log line into the document ready block to make sure it's being fired. Something like this:

<script type="text/javascript">
$(document).ready(function($) {
   $('#fb_iframe').contents().find('#customer-name').val( $('#custname').val() );
   console.log( $('#custname').val() );
});
</script>

Open in new window


If the doc.ready block is being fired, you'll see John Doe being output to the console, which would confirm my suspicions that it's being fired too early.

If that is the case, we'll need to find another solution.
Avatar of Tammu

ASKER

Wow thanks for looking into it again, and yes its being fired too early as the console returns John Doe. I wondering if there is way to grab the main page value from the iframe like the iframe is ready or onload grab the value

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tammu

ASKER

Just Freaking Brilliant :) Thanks a bunch