Say I have an element like :
var myElem='<div class="oldClass" style="color:green;" id="oldId"><span>This DIV has some
innerHTML as well</span></div>'
And a JSON like:
var myJson = {
"class": "myClass",
"id": "myId",
"style": "border: 1px solid black; color: red; font-size:20px;"
};
Now I need to update myElem with data in JSON.
What I tried is:
for(var key in myJson){
var attrName = key;
var attrValue = myJson[key];
console.log('attrName ', attrName);
console.log('attrValue ', attrValue);
$(myElem).removeAttr(attrName);
$(myElem).attr(attrName, attrValue);
}
My expectation from this code:
myElem = '<div class="myClass" style="border: 1px solid black; color: red; font-size:20px;" id="myId"><span>This DIV has some innerHTML as well</span></div>'
However, this is not working.myElem remains what it was initially.
myElem and myJson both are dynamic and not static
Can anyone please tell what am I doing wrong? (I know I'm doing something wrong but I'm unable to find it)!