Ben
Ben

Reputation: 85

use json value as getElementById identifier

I have several hidden hard coded div's are at the bottom of my page that get injected with values on the initial page load

<div id="item_name1">123</div>
<div id="item_name2">456</div>
<div id="item_name3">789</div>
<div id="item_name4">123</div>
// etc etc  

separately and later, I get JSON data and inject more html into the page

how can I select and use one of the hidden div values based on the JSON data values ?

$.each(data,...........

var test = data[i].item_name2; 
var x = document.getElementById(test).innerHTML;
...
// then use `x` later 

Upvotes: 1

Views: 587

Answers (1)

Lix
Lix

Reputation: 48006

If you want to get a jQuery object then you'll have to use the jQuery variable - $ in most cases.

$.each(data,function(i,elem){
  var test = data[i].item_name2; 
  var x = $("#"+test);
  ...
});

You'll then have the object x to perform jQuery functions.

Upvotes: 2

Related Questions