MiddleCloud
MiddleCloud

Reputation: 210

How to build an array with jquery and make input fields

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!

Upvotes: 0

Views: 221

Answers (2)

Muthukumar M
Muthukumar M

Reputation: 1126

 $('.row-dp').each(function(){
   if($(this).attr('id') && $(this).attr('id')!="")
    $('body').append("<input type='text' id="+$(this).attr('id')+" val="+$(this).text()+">");
 });

Upvotes: -1

js1568
js1568

Reputation: 7032

$('.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()));
});

Upvotes: 2

Related Questions