jquery - javascript variable to call object function -
this question has answer here:
i'm new oop, please excuse me if terminology off. i'm trying use function parameter call object argument.
i think more understandable sample code:
js
$(".color").click(function() { var newcolor = $(this).attr("data-color"); functions.colors.show(newcolor); }); var functions = { colors: { show: function(newcolor) { $("h1").text(mytexts.test.newcolor); } } } // end functions var mytexts = { test: { red: "bright red", green: "grassy green", blue: "sky blue" } } // end texts
as can see, i'm trying get, say, "sky blue", show inside h1. however, when click, say, show "bright red", not working.
as reference, here html:
html
<span class="color" data-color="red">red</span> <span class="color" data-color="green">green</span> <span class="color" data-color="blue">blue</span>
from understanding, when click color, color (data-color attribute) parsed "show()" function, can't proper text show up.
why ?
try use bracket notation
@ context,
$("h1").text(mytexts.test[newcolor]);
full code,
var functions = { colors: { show: function(newcolor) { $("h1").text(mytexts.test[newcolor]); } } }
and should use .data(key)
while fetching data attribute..
var newcolor = $(this).data("color");
Comments
Post a Comment