DotDragnet
May 24, 2012, 07:23:37 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: arrays/objects in javascript  (Read 172 times)
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« on: September 27, 2011, 03:14:57 PM »

So, I want to set up an associative array (although I've read I should use Object rather than Array for this), and then access the value that matches the key I have. In PHP I would use

Code:
$options = array();
$option['A'] = "available";
$option['U'] = "unavailable";
etc...

$key = "U";
$class = $option[$key] // sets class to 'unavailable'

At present I've got in JS

Code:
var options = new Object();
options['A'] = 'available';
options['U'] = 'unavailable';
etc.

var key = "U";
var class = ?

How do I set the class to equal options['U']? I've looked through various posts and help on the subject and am starting to get lost with various functions etc. I have jQuery in use too if that's easier.

And whilst we're on the subject, is it possible get the key if you know the value?

tyvm smile
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #1 on: September 27, 2011, 03:33:16 PM »

So I've found this on the jQuery site

Code:
var options = {
   A: "available",
   U: "unavailable"
 }
 var key = "U";
 var class = options[key];
 $('#test').text(class);

(modified from the array notation - http://api.jquery.com/Types/#Object)

Yet it doesn't set the div with id test to contain the text. Not sure why not Sad  Really feel like I'm having a blonde day today (then again, am very sleep deprived so apologies if the answer is obvious)
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #2 on: September 27, 2011, 07:36:34 PM »

d'oh, okay so 'class' is a reserved word. It just came to me  Roll Eyes
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!