469,361 Members | 2,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

Associative array key

I need help, im sure the solution is simple, but it's driving me nuts. Im trying to retrieve the value of js_array['pearl']['a'] using js_array['pearl'][selectedObj.selectedIndex]; where selectedObj.selectedIndex is a string value from a select box. I need to wrap quotes around selectedObj.selectedIndex for it to retrieve the associative key properly, but everything ive tried has failed. This works fine if the key is numeric, but fails if the key is a string. I'm sorry if I didn't explain this very well, my brain is kind of fried today. Can someone please help me???


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language='JavaScript'> 
  3. var js_array = new Array();
  4. js_array['stain'] = new Array();
  5.  
  6.  
  7. js_array["pearl"]["a"] = "pearla.jpg";
  8.  
  9.  
  10. function pearlchange(selectedObj){
  11. document.pearlimg.src=imgDir + js_array['pearl'][selectedObj.selectedIndex];
  12. }
  13. </script>
  14.  
  15. <form>
  16. <select name="pearl" onchange='pearlchange(this)' >
  17. <option value='a'>Violet (a)</option>
  18.  
  19. </select>
  20. </form>
  21.  
  22.  
  23. <img name='pearlimg' src="" />
  24.  
Jan 30 '09 #1
7 2290
acoder
16,027 Expert Mod 8TB
Use selectedObj.value in place of selectedIndex.
Jan 30 '09 #2
gits
5,390 Expert Mod 4TB
just a note:

JavaScript has no associative Arrays ... even when you do what you did. In fact you extend the native JavaScript array with new properties ... and this is a 'misuse' of the array as an object ... so the correct way would be to use an object at all. the misuse could lead to strange behaviour when you use for-in-loops or rely on array-methods or properties ... for example length will be zero.

so the correct way would be ... the alert shows the construct in FF:

Expand|Select|Wrap|Line Numbers
  1. var foo = {};
  2. var bar = {};
  3.  
  4. bar.key1 = 'foobar';
  5.  
  6. foo.foo_key1 = bar;
  7.  
  8. alert(foo.toSource());
kind regards
Jan 31 '09 #3
Thanks for the help, much appreciated.

Justin
Feb 2 '09 #4
I have another problem now. FF doesn't like it when I assign a numeric "key" to my object. What am I doing wrong here?

Expand|Select|Wrap|Line Numbers
  1. var stain = {};
  2. var pearl = {};
  3. stain.1 = 'stain1.jpg';  //This produces a 'missing ; before statement' error
  4. pearl.a = 'pearla.jpg'; //This works fine
  5.  

I'm also having problem with using a variable as my object "key" value:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var optionImg = {};
  3. var pearl = {};
  4. pearl.a = 'pearla.jpg';
  5. pearl.b = 'pearlb.jpg';
  6. pearl.c = 'pearlc.jpg';
  7. pearl.e = 'pearle.jpg';
  8. pearl.f = 'pearlf.jpg';
  9. pearl.g = 'pearlg.jpg';
  10. pearl.h = 'pearlh.jpg';
  11. optionImg.pearl = pearl;
  12. alert(optionImg.toSource());
  13. var imgDir='http://vikingcue.com/images/options/';
  14.  
  15.  
  16. function pearlChange(selectedObj){
  17. var testpath=imgDir + optionImg.pearl.selectedObj;
  18. alert(selectedObj in optionImg.pearl.toSource());
  19. alert(testpath.toSource());
  20. }
  21.  
I need the selectObj variable to reference the key, instead I get the error: optionImg.pearl.selectedObj is undefined. I'm sorry if these questions are elementary, I'm just trying to learn a few things here.


Here is the select box:

<select onChange='pearlChange(this.value)>
<option value='a'>Pearl A</option>
</select>

Thanks,

Justin
Feb 3 '09 #5
acoder
16,027 Expert Mod 8TB
Use square bracket notation:
Expand|Select|Wrap|Line Numbers
  1. optionImg.pearl[selectedObj]
Feb 3 '09 #6
Thanks, that fixed both problems I was having.
Feb 3 '09 #7
acoder
16,027 Expert Mod 8TB
You're welcome. Glad it did!
Feb 3 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

27 posts views Thread by Abdullah Kauchali | last post: by
6 posts views Thread by mark4asp | last post: by
4 posts views Thread by Robert | last post: by
8 posts views Thread by Derek Basch | last post: by
5 posts views Thread by soup_or_power | last post: by
7 posts views Thread by Robert Mark Bram | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
11 posts views Thread by Bosconian | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.