www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Get value chosen from select, display the value of the array with that same name.

  1. #1
    Join Date
    Feb 2011
    Posts
    4

    resolved [RESOLVED] Get value chosen from select, display the value of the array with that same name.

    I am working on a little project here. I have two arrays (States, Cities) I am trying to allow the user to chose one from a select form (Choice) and then have the values of the chosen array displayed in a alert(). Obviously I could just write an if statement and display the required content depending on whether the value selected == States or Cities. But this project is just a placeholder project for me to figure out one aspect of a much larger project I am working on. And it is important for the result to be chosen dynamically as in the final project I won't know the values that the select field is being filled with.

    What I need to do is take the value returned from the select field (Choice) and turn that into the name of the array and display that. If I use this code
    PHP Code:
    alert(States); 
    then it returns the values of that array. But like I said I need to be able to dynamically select what array to display based on users input

    PHP Code:
    alert(document.getElementById("Choice").value); 
    returns the value chosen. How can I take the value chosen and actually display the value of the array with that same name. In other words let JS know that I don't literally want it to display the value chosen but rather the value chosen is the name of the array I want it to display?

    PHP Code:
       1.
          Pseudo code
       2.
          x 
    document.getElementById("Choice").value;
       
    3.
          x is name of 
    array;
       
    4.
          alert
    (x); 
    Thanks

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    If your arrays are Global variables, they are also properties of the Global window object:
    Code:
    var x = document.getElementById("Choice").value
    var chosenArray=window[x];
    If not, post your code to see what other options we might have

  3. #3
    Join Date
    Feb 2011
    Posts
    4

    Thanks

    Put that in there and it works fine, shows my the values of the array that is selected. Thanks, guess I need to Google the window object, any advice for a starting point?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by jwiere03 View Post
    Put that in there and it works fine, shows my the values of the array that is selected. Thanks, guess I need to Google the window object, any advice for a starting point?
    I am afraid you won't find this in tutorials. At least not in an explicit way. But the relationship between the entities (variables, objects, primitives, properties, values, functions, methods...) is simple in JavaScript: everything is an object, or can be treated as an object, or transformed in an object, and all the objects can be express as being the properties of a comprehensive "parent" object. The ultimate Object is window
    Last edited by Kor; 02-15-2011 at 03:42 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles