SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementByTag('select') into an array

    hi everyone,

    im a bit new to javascript and would like some help with something...

    i have a web page with several drop down menus on three pages.
    i want the browser to remember these values by writing their values to a cookie.

    this is the code i have come up with which is in my init():

    //set value to current value for that cookie
    var streewberryBlitzs = cookieVal("streewberryBlitzCookie");
    document.getElementById("sb").value = streewberryBlitzs;


    var selectValue = document.getElementById("sb");
    selectValue.onchange = quantityHandler;

    function quantityHandler()
    {
    streewberryBlitzs = document.getElementById("sb").value;
    document.cookie = "streewberryBlitzCookie=" + streewberryBlitzs;
    }

    ///////////////////////

    i have 18 other drop down boxes on three pages so i dont really want to have to write this out 18 times, once for each item.

    what i want to be able to do is right a function that does something like

    1. gets the id of each select [getElementByTag('select')] as 'xxxxxx' and puts in an array.
    2. and then rewrite to something like so i can work out for each:


    var xxxxxx = cookieVal("xxxxxxCookie");
    document.getElementById("xxxxxx").value = xxxxxx;


    var selectValue = document.getElementById("xxxxxx");
    selectValue.onchange = quantityHandler;

    function quantityHandler()
    {
    xxxxxx = document.getElementById("xxxxxx").value;
    document.cookie = "xxxxxxCookie=" + xxxxxx;
    }

    ////////



    hope u get what im on about. if not, ask and i can explain further.

    best wishes

    Paul

  2. #2
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    ...actually declaring a variable like this seems to work:

    var smoothies = document.getElementsByTagName('select');


    i can then for loop threw each element and do what i want

    for (smoothies[count]; count <= allElements; count++)
    {
    //magic
    }

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Looping through a nodelist like that is perfectly valid since nodelists do allow you to step through them one entry at a time in a loop using the nodelist length to determine how many times you want to loop. If you want to use any of the array methods you'd need to copy the nodelist into an array first.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •