0

Like i sayed in the title, i need to build an array and change value in form input field's the code is:

<div class="row-dp">
 <div class="cel-1">Obiectiv</div>
 <div class="cel-2" id="obiectiv">macarale</div>
 <div class="cel-3">&nbsp;</div>
</div>
<div class="row-dp">
 <div class="cel-1">Orasul selectat</div>
 <div class="cel-2" id="orasulselectat">Bucuresti</div>
 <div class="cel-3">&nbsp;</div>
</div>

I need to take from " <div class="cel-2" " id to be a key and value to be value

an example

VAR['obiectiv'] = macarale;
VAR['orasulselectat'] = Bucuresti;

to transform from value in input field like

<input name="obiectiv" value="macarale" type="text" />

Thank you!

asked Jun 18, 2012 at 13:23
1
  • Do all the input elements already exist or do they need to be created? Commented Jun 18, 2012 at 13:30

2 Answers 2

2
$('.cel-2').each(function() {
 $('input[name='+this.id+']').val($(this).text());
});

If you have to create the input elements, use this:

$('.cel-2').each(function() {
 $('#yourFormNameHere').append($('<input/>').prop({'name':this.id,'type':'hidden'}).val($(this).text()));
});
answered Jun 18, 2012 at 13:29
Sign up to request clarification or add additional context in comments.

1 Comment

it create the input with value and id from the data at the last div
-1
 $('.row-dp').each(function(){
 if($(this).attr('id') && $(this).attr('id')!="")
 $('body').append("<input type='text' id="+$(this).attr('id')+" val="+$(this).text()+">");
 });
answered Jun 18, 2012 at 13:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.