SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: value to array

  1. #1
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    value to array

    I need to build an array from the value e.g.

    array = form value
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  2. #2
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    e.g.

    array = new array()
    array[0] = "jo";
    array[1] = "no";

    value=array

    looad array = value
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  3. #3
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have some real time to myself to ask explain heres my example

    <SCRIPT>
    woof =new Array()
    meow =new Array()
    squeak =new Array()

    function load_array()
    {
    var animal = array(document.form1.ani.options.selectIndex.value);
    }
    </SCRIPT>

    <FORM name="form1">
    <SELECT onChange="load_array()">
    <OPTION value="woof">Dogs</OPTION>
    <OPTION value="meow">Cats</OPTION>
    <OPTION value="squeak">Mouse</OPTION>
    </SELECT>
    </FORM>

    any ideas
    help will be appriciated
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  4. #4
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function will do what you want.

    Code:
    function get_val(){
    	obj = document.forms[0].elements[0];	//The dropdown
    	sel=[];
    
    	sel[0]=obj[obj.selectedIndex].value;	//Option Value
    	sel[1]=obj[obj.selectedIndex].text;	//Option Text
    
    	return sel;
    }
    Drinky

  5. #5
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a fully working example, cut and paste into a new blank html doc. When you select a value in either text box it will show you what you picked in a message box

    Code:
    <html>
    <head>
    <title>Select info demo</title>
    <script>
    	function get_val(obj){
    		sel=[];
    
    		sel[0]=obj[obj.selectedIndex].value;		//Option Value
    		sel[1]=obj[obj.selectedIndex].text;		//Option Text
    
    		//return sel;
    		alert('Value:\t'+sel[0]+'\nText:\t'+sel[1])
    	}
    </script>
    </head>
    
    <body>
    
    <form>
    	<select onchange="get_val(this)">
    		<option value="val0">text0</option>
    		<option value="val1">text1</option>
    		<option value="val2">text2</option>
    		<option value="val3">text3</option>
    	</select>
    
    	<select onchange="get_val(this)">
    		<option value="val4">text4</option>
    		<option value="val5">text5</option>
    		<option value="val6">text6</option>
    		<option value="val7">text7</option>
    	</select>
    </form>
    </body>
    </html>
    Drinky

  6. #6
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I mean like the values are

    woof
    google
    meow

    and three arrays with them

    woof[]
    google[]
    meow[]

    and I want to know how to get the array that is named after the value so I can use it to write a combo-box control
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  7. #7
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the eval(String) function to pickup objects variables and execute functions whose names correspond to the supplied string.

    Off the top of my head this code should return your arrays based on supplying the words woof, google, meow to it.

    Code:
    var woof=[]
    var google=[]
    var meow=[]
    
    function return_array(val){
    	if(typeof eval(val) is "object"){
    		return eval(val);
    	} else {
    		return false;
    	}
    }
    
    //useage
    woofArray = return_array('woof');
    Drinky

  8. #8
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My whole page it still isn't working
    Code:
    <HTML>
    <HEAD>
    <TITLE>LANGUAGE</TITLE>
    
    <SCRIPT language="javascript">
    <!--hide from old grannies
    var volks = new Array();
    volks[0] = "Beatle";
    volks[1] = "Sharan";
    volks[2] = "Passat";
    
    var alltogether;
    
    function return_array(val){
    if(typeof eval(val) is "object"){
    return eval(val);
    } else {
    return false;
    }
    }
    
    function add_options(typelist)
     {
      if( typelist == "model" )
      {
      var typelist = window.document.boogle.google;
      var dropdownchange = window.document.boogle.noodle;
      }
    
      var typelist = return_array(typelist[typelist.selectedIndex].value);
      
      for(var i=0;i <dropdownchange.length ; i++)
       { 
        dropdownchange.options[i].text = typelist[i];
       }
      while(dropdownchange.length <typelist.length)
       {
        var new_option = new Option;
        new_option.text = typelist[dropdownchange.length];
        dropdownchange.options[dropdownchange.length]= new_option;
       }
      }
    //let them put there glasses on -->
    </SCRIPT>
    
    </HEAD>
    <BODY>
    <FORM name="boogle" onSubmit="return false;">
    <SELECT name="google"  onChange="add_options("model");">
    
    <OPTION value="make">---Make---</OPTION>
    <OPTION value="volks">Volkswagen</OPTION>
    
    </SELECT>
    
    <SELECT name="noodle">
    
    <OPTION value="model">---Model---</OPTION>
    
    </SELECT>
    
    <INPUT type="submit" value="Search">
    </FORM>
    
    </BODY>
    </HTML>
    any ideas why?
    Last edited by Da Mouse; Aug 30, 2002 at 05:45.
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  9. #9
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help with this?
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  10. #10
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ::Bump::

    I really need an answer for this
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can definately help you with the array thingy - if it's not already solved.

    Just to clarify though:

    What part of the page is not working ?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I select the first box the second one does nuttin
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  13. #13
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put some alerts in the function and function logic to see what it's doing.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  14. #14
    SitePoint Zealot Da Mouse's Avatar
    Join Date
    Aug 2002
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    (typeof eval(val) is "object")
    Should've been
    Code:
    (typeof eval(val) == "object")
    got that from a different forum
    JavaScript WorkShop
    go there for ANY javascript needs its excellent
    I am DaMouse
    Nobody can beat me so long as I'm stubborn -DaMouse
    Squeak

  15. #15
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Da Mouse
    Code:
    (typeof eval(val) is "object")
    Should've been
    Code:
    (typeof eval(val) == "object")
    got that from a different forum
    JavaScript WorkShop
    go there for ANY javascript needs its excellent
    My fault answering questions while tired
    Drinky


Bookmarks

Posting Permissions

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