0

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.

3
  • My quick guess is that it's the spaces around the = for the value attribute. Commented Oct 13, 2016 at 21:30
  • 1
    Seems to work -> jsfiddle.net/adeneo/m3Lgse47 Commented Oct 13, 2016 at 21:31
  • 1
    Your quotes in the object literal are invalid. Commented Oct 13, 2016 at 21:32

2 Answers 2

1

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())
});
Sign up to request clarification or add additional context in comments.

Comments

0
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);

Comments

Your Answer

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.