1

I don't seem to understand how to access an objects value properly.

My object:

  // countrycode: "Radio station name"
  var radioStations = {
    fi: "Foo",
    hu: "Bar",
    am: "Baz"
  };

Then I have a variable called code which comes from a jQuery plugin, and has the countrycode of the country the user is mouseovering on a vector map.

I'd need to use code to add the radio stations name into the tooltip in here:

onLabelShow: function(event, label, code){
  if ( code in radioStations ) {
    label.text(radioStations.code); // <- doesn't work
  } else  { // hide tooltips for countries we don't operate in
    event.preventDefault();
  }
},
1
  • radioStations.code -> JavasScripts looks for property 'code' of object radioStations, better radioStatios[code] code is then evaluated. Commented May 25, 2012 at 9:05

2 Answers 2

6

You need to us array notation to access the object by a variable. Try this:

onLabelShow: function(event, label, code){
    if (code in radioStations) {
        label.text(radioStations[code]);
    } 
    else  { 
        event.preventDefault();
    }
},

Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

You can use:

onLabelShow: function(event, label, code){
  if(radioStations[code]) {
   label.text(radioStations[code]);
  } else {
   event.preventDefault();
  }
}

DEMO

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.