Dynamically calling JSON data using jQuery & HTML5 data-* -
i'm working expressionengine , have converted channel entries json, works nicely.
what trying populate sweet alert 2 overlay information specific json objects using id stored in data-*
attribute.
here example of json:
var director_45 = { "title": "andy h", "entry_id": 45, }
and if simple jquery alert this, returns name:
alert(director_45.title)
however, if in jquery:
$('.trigger-director').on('click', function() { var director_id = $(this).data('director'); var director = 'director_' + director_id; alert(director.title); });
with html fire it:
<div class="col-xs-6 col-md-3"> <div class="director-box"> <img src="/images/made/images/uploads/images/andy_400_300_c1.jpg" class="img-responsive" width="400" height="300" alt="" /> <h3>andy h</h3> <p>director</p> <a class="trigger-director" data-director="45">find out more</a> </div> </div>
the alert upon clicking 'find out more' link shows "undefined".
i've created jsfiddle link here - https://jsfiddle.net/zu103vxc/
any idea i'm doing wrong and/or missing?
use eval()
var director_45 = { "title": "andy h", "entry_id": 45, } alert(director_45.title) $('.trigger-director').on('click', function() { var director_id = $(this).data('director'); var director = 'director_' + director_id; alert(eval(director).title); });
or global variables
var director_45 = { "title": "andy h", "entry_id": 45, } window.director_45 = director_45; alert(director_45.title) $('.trigger-director').on('click', function() { var director_id = $(this).data('director'); var director = 'director_' + director_id; alert(window[director].title); });
Comments
Post a Comment