jquery - Access javascript objects dynamically -


i have javascript object want access dynamic variable:

var color = {                 red : '#ff0000',                 black : '#000000',                 silver : '#7d8989'             };  var currentcolor = $(val).html();  console.log(color[currentcolor]); 

but undefined message console. how can access data?

edit:

problem option value had whitespace @ end, fast reply:

var currentcolor = $.trim($(val).html()); 

this works. comment, i'm not sure "val" is, if it's id, make sure it's targeted correctly.

html:

<p id="val">red</p> 

js:

var color = {                 red : '#ff0000',                 black : '#000000',                 silver : '#7d8989'             }; var currentcolor = $("#val").html(); console.log(color[currentcolor]); 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -