Gaurav Saxena
Gaurav Saxena

Reputation: 1

How to use input text tag inside map in javascript

I am trying to traverse my map on javascript to use the keys and values to form the input type="text" 's id and name = map's key and value = tempMap[key]

var tempMap = {"user" : “abc”,"password" : “xyz”,"Month” : "1", “Year” :“2016”}

for(item in tempMap){
        var itemVal = tempMap[item];
         var defaultRow = $('<tr> '+
                 '<td> '+
                   '<div class="mno">'+
                       '<label style="width: 150px;">'+item+':'+'</label>'+
                       '<input type="text" name="'+item+'" id="' +item+ '" value = "' +itemVal+ '" />'+
                      '</div>'+
                    '</td>'+
                  '</tr>' );

    }

for label it is working good, but its not working for input tag. How can I achieve this.

Upvotes: 0

Views: 1023

Answers (2)

Gaurav Saxena
Gaurav Saxena

Reputation: 1

Thank you all.

I used the below code and it worked fine now:

var resolvedStr =  '<tr> '+
        '<td> '+
        '<div class="applicationsShow-div-left">'+
            '<label style="width: 150px;">'+item+':'+'</label>'+
            '<input type="text" name="itemName" id="itemId" value="itemVal"/>'+
           '</div>'+
         '</td>'+
       '</tr>';
       resolvedStr = resolvedStr.replace("itemVal",itemVal);
       resolvedStr = resolvedStr.replace("itemId",item);

Upvotes: 0

Jacopo Brovida
Jacopo Brovida

Reputation: 465

Your code work. Another solution can be:

var tempMap = {"user" : "abc","password" : "xyz","Month" : "1", "Year" :"2016"};
$.each(tempMap,function(key,value){
var defaultRow = $('<tr> '+
             '<td> '+
               '<div class="applicationsShow-div-left">'+
                   '<label style="width: 150px;">'+key+':'+'</label>'+
                   '<input type="text" name="'+key+'" id="' +key+ '" value = "' +value+ '" />'+
                  '</div>'+
                '</td>'+
              '</tr>' );

alert(defaultRow.html())
});

Upvotes: 1

Related Questions