I don't know if this is possible or how to do it but what I want to do loop through some elements with a class, get the id and see if the value of the id is a key name in a JSON object. If it is, I want to get the corresponding value and pass it back to the elements HTML.
var options = {
"test1": "something",
"test2": "something else",
"test3": "something amazing",
}
$('.myclass').each(function(i, val) {
if(options.hasOwnProperty(val.id)) {
$('#' + val.id).html(options.val);
}
});
<p class="myclass" id="test1"></p>
<p class="myclass" id="test2"></p>
<p class="myclass" id="test3"></p>
The problem is with this line specifically options.val. I was thinking about a variable variable but don't know how to do that in javascript or looking for another way.
$('#' + val.id).html(options.val);