0

I have a javascript object which I want to access by a dynamic variable:

var color = {
                red : '#ff0000',
                black : '#000000',
                silver : '#7d8989'
            };

var currentColor = $(val).html();

console.log(color[currentColor]);

But I get undefined message by the console. How can I access the data?

EDIT:

Problem was that the option value had whitespace at its end, thanks everyone for fast reply:

var currentColor = $.trim($(val).html());
9
  • 6
    what is $(val).html(); Commented May 10, 2013 at 14:01
  • 1
    Did you check what the value for currentColor is before you get the value from the color-object. If you specify an existing property name, this should work. Commented May 10, 2013 at 14:01
  • This should work if val.html is "red" etc Commented May 10, 2013 at 14:01
  • 3
    Maybe you want to select an Element with the id "val" if so -> $("#val").text() Commented May 10, 2013 at 14:01
  • 1
    To get the value of a select you want .val() not .html()! Commented May 10, 2013 at 14:05

6 Answers 6

2

This works. Like the comment, I'm not sure what "val" is, but if it's an 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]);
Sign up to request clarification or add additional context in comments.

1 Comment

@blachawk Dot notation isn't possible with a dynamic value. There is no currentColor property of color - you have to use bracket notation like they have it
0

You maybe want to trim your string before finding the color in your map:

var color = {
    red : '#ff0000',
    black : '#000000',
    silver : '#7d8989'
};

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

console.log(color[currentColor]);

And it's not very fast to route your data through the DOMNodes like that.

Comments

0

Your code seems to be working fine, so it is probably something wrong with your selector See http://jsfiddle.net/9GPbv/1/ works fine

<div class="val">red</div>

var color = {
                red : '#ff0000',
                black : '#000000',
                silver : '#7d8989'
            };

var currentColor = $(".val").html();

alert(color[currentColor]);

Also if your using an input like at http://jsfiddle.net/tMFZr/ you should use .val() not .html()

Comments

0

You should use quotes in your object since you will be using it as string values: HTML

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

JS

var color = {
                'red' : '#ff0000',
                'black' : '#000000',
                'silver' : '#7d8989'
            };

var currentColor = $(val).html();

console.log(color[currentColor]);

3 Comments

But then please use var val = "#val"
Sure, but it stays a bad practise inherited from faulty IE
in my opinion, not to use var is a bad practice, having your IDs as references to elements is another subject.
0

I think you are getting the undefined in the chrome console. IF so please take a look at What does it mean if console.log(4) outputs undefined in Chrome Console?

Comments

0

If your html have an id:

var color = {
    red : '#ff0000',
    black : '#000000',
    silver : '#7d8989'
};
var currentColor = document.getElementById("val").innerText;
console.log(currentColor);
console.log(color[currentColor]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.