Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create form hidden inputs from jquery object(array)

Status
Not open for further replies.

sdagger3

Programmer
Apr 14, 2011
8
GB
I have created a jquery object (array) of items in a parent page which is passed into an iframe. Example code used is something like:

<script type="text/javascript">
$(document).ready(
function()
{
$.SiteInfo = {custLabels:[{'xx_fruit':varFruit},{'xx_vegetable':varVegetable },{'xx_car':varCar },{'xx_plane':varPlane }]};

}
);
</script>

What I’m trying to do in the iframed page containing a form is to also create hidden input fields with the custLabels so it will look like:

<INPUT TYPE="HIDDEN" NAME="xx_fruit" VALUE="custLabels(xx_fruit)">
<INPUT TYPE="HIDDEN" NAME="xx_vegetable" VALUE="custLabels(xx_vegetable)">
<INPUT TYPE="HIDDEN" NAME="xx_car" VALUE="custLabels(xx_car)">
<INPUT TYPE="HIDDEN" NAME="xx_plane" VALUE="custLabels(xx_plane)">

So an example output would be (assuming varFruit is apple, varVegetable is carrot and so on…):

<INPUT TYPE="HIDDEN" NAME="xx_fruit" VALUE="apple">
<INPUT TYPE="HIDDEN" NAME="xx_vegetable" VALUE="carrot">
<INPUT TYPE="HIDDEN" NAME="xx_car" VALUE="ford">
<INPUT TYPE="HIDDEN" NAME="xx_plane" VALUE="airbus">

Could you possibly help on how I can get this data out of the object and into the separate fields? My JavaScript is not very good.
 
You can't run code inside value properties of elements as they are just interpreted as strings. You could if you set your jquery object correctly, access the values in the ready event, and set the inputs value there.


Code:
 $(document).ready(
    function()
        {
        		var varFruit = "apple";
                        ...
        		var custLabels={'xx_fruit':varFruit,'xx_vegetable':varVegetable ,'xx_car':varCar ,'xx_plane':varPlane };
            $SiteInfo=$(custLabels);
[green]//set input with name xx_fruit to value of xx_fruit property of jquery object SiteInfo[/green]
            $('input[name="xx_fruit"]').val($SiteInfo.prop('xx_fruit'));
								
				 
        }
    );

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Vacunita

That is really helpful. I also managed to do it this way before you replied which works too:

var $ = window.parent.$;
var SiteInfo= $.SiteInfo;

var d,o=SiteInfo.custLabels;
for(var p in o){
if(o.hasOwnProperty(p)){
d=o[p];
if(d && typeof d=='object'){
for(var t in d){
if(d.hasOwnProperty(t)){
document.write('<input type="hidden" name="' + t + '" value="' + d[t] +'">');
console.log(d[t]);
console.log(t);
}
}
}
}
}
 
No, prob. One a side note, Document.write will generate a new blank document when it's called after the page has loaded, so caution s advised when using it.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top