SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Take data from an array and apply each array item to an element

    I'm trying to grab values from a set of arrays based on the value returned by my select box.

    **Caveat - this is not an area I have any real experience with**

    My arrays look like:
    Code JavaScript:
    /*
    var fees_# = ["tuition", "fees", "total"];
    */
    var fees_1 = ["38,200", "2,350", "40,550"];
    var fees_2 = ["19,750", "2,350", "22,100"];
    var fees_3 = ["38,700", "2,450", "41,150"];
    var fees_4 = ["19,450", "1,900", "21,350"];
    var fees_5 = ["30,000", "5,100", "35,100"];
    //and so on
    My select will be returning values like:
    degree_1a
    degree_2a
    degree_3a
    degree_4a

    degree_1b
    degree_2b
    degree_3b
    degree_4b

    degree_1c
    degree_2c
    degree_3c
    degree_4c
    /...
    degree_1z
    degree_2z
    degree_3z
    degree_4z

    degree_1aa
    degree_2aa
    degree_3aa
    degree_4aa
    /...etc

    I then need to test for each, then associate with one of my fees arrays, then grab each of the values in the array and write those values to elements within my page.

    I'm then doing this to evaluate for each degree
    Code JavaScript:
    $(document).ready(function() {
    	$('select#degree').change(function(){
    		var myValue = $('select#degree').val();
    		if(myValue == 'degree_1a'){alert(myValue);}
    		if(myValue == 'degree_2a'){alert(myValue);}
    		if(myValue == 'degree_3a'){alert(myValue);}
    	});
    });

    I need to first figure out how best to import all of these 60+ arrays and then in each of my conditions pull out each value and write to my page.

    There is a unique 1 to 1 relationship between each degree and array so I can't consolidate as the values for each degree differ slightly.

    Any tips/hints/help would be greatly appreciated.

    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not totally clear on what you want, but I'm gonna throw this out there. If I'm wrong, please try to clarify.

    Code:
    var fees = {
        degree_1a: ["38,200", "2,350", "40,550"],
        degree_1b: ["19,750", "2,350", "22,100"]
    };
    
    var myValue = "degree_1b";
    
    alert(fees[myValue])

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2006
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for responding.

    This looks like exactly what I need - I think this will work.

    The other thing I need to do is pull each of the array item values out of the selected array and display those values in elements within my page.

    if my array breaks down like:
    Code JavaScript:
    degree = ["tuition", "fees", "total"];

    I need to display the respective values like:
    Code HTML4Strict:
    <input type="text" name="tuition" value="" readonly="readonly" />
    <input type="text" name="fees" value="" readonly="readonly" />
    <input type="text" name="total" value="" readonly="readonly" />

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you give each of those form elements a unique id attribute, then you can do something like this for each of them
    Code:
    document.getElementById("tuition").value = fees[myValue][0];


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
  •