jquery - JavaScript scope with JSON object -


this function called when element clicked. creates string based on element clicked, , looks value associated string in json object. alerts value. here's how looks:

function alerthelp(){     var option = $(this).context.parentnode.textcontent.substring(0, $(this).context.parentnode.textcontent.length - 2);     $.getjson("messages.json", function(json, option) {         alert(json[option]);     }); } 

what i've found option set correctly, when passed function alert changed. if alert(option) right before alert(json[object]);, alert says "success". not sure what's that. alert(json[option]) alerts "undefined".

here's messages.json:

{     "space control": "red = square black controls. green = square white controls. yellow = square contested both players",     "legal moves": "click on piece , blue dot appear on squares piece can move to",     "pieceflair": "when move piece, pieces come under attack direct result of piece moved pulse white",     "forks": "all moves result in simultaneous attack of 2 pieces shown",     "pins": "not yet implemented" } 

you have "shadowed" var option function argument option, try instead:

var text = $(this).context.parentnode.textcontent; var option = text.substring(0, text.length - 2);  $.getjson("messages.json", function(json, status) {     alert(status);  // success     alert(json[option]); }); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -