Avatar of niceoneishere
niceoneishere
 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
JavaScriptHTMLjQuery

Avatar of undefined
Last Comment
niceoneishere

8/22/2022 - Mon
Chris Stanyon

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

niceoneishere

ASKER
Thanks for reply but does not work. its not setting the value.
Chris Stanyon

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
niceoneishere

ASKER
Thanks again, can I send a private message with my site info.
Chris Stanyon

Yeah sure.
niceoneishere

ASKER
Thanks PM sent
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Stanyon

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.
niceoneishere

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
Chris Stanyon

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
niceoneishere

ASKER
nope that help. I checked in chrome and it says now jQuery is undefined. Appreciate it
niceoneishere

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
Chris Stanyon

Worked fine for me:

HTML of the form fields
Result of the form submit
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
niceoneishere

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

html
results
Chris Stanyon

Not sure. I would make sure you've cleared down your cache and check there are no errors showing up in the console.
niceoneishere

ASKER
hmmm very strange I even tried in 2 different computers and same result :(
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Chris Stanyon

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.
niceoneishere

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
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
niceoneishere

ASKER
Just Freaking Brilliant :) Thanks a bunch
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.