SitePoint Sponsor

User Tag List

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

    calling a function onchange with a parameter

    hmmmm, basically what i am trying to do is make an array of all the drop down menus (select tags) on the page, and store the values 0 - 9 to a cookie when it is changed.

    the entire code i have written is below. it all works (almost!) and you can add this to any page and it will set up as long as the select s have an id (<select id="xxx">).

    there is however, one problem. at the moment it only records the last select properly. this is because i have set the selectID var as a global. this means when it gos threw the for loop it keeps the value of the last select.

    i set up selectID as a global as i couldnt work out how to call the function updateCookie by parsing a parameter to it.

    selectValue.onchange = updateCookie;

    if i...
    selectValue.onchange = updateCookie(selectID);
    ...and update the updateCookie function to except this, it doesnt like it.

    im pretty sure if i could work out another way to say...
    selectValue.onchange = updateCookie;
    ...while parsing the id as a parameter, i could get this working



    window.onload = init;

    //declare selectID as a global variable
    var selectID = "";

    function init()
    {

    //create a variable of all the select tags - this method acts like an array in this instance hence the name
    var selectArray = document.getElementsByTagName('select');

    for(selectArray[count=0]; count < selectArray.length; count++)
    {
    //set selectID and selectCookie variables
    selectID = selectArray[count].id;
    var selectCookie = selectID+"Cookie";

    //get the value of the specified cookie
    var selectCookieValue = getCookieValue(selectCookie);
    //set the drop down to show the cookies value
    document.getElementById(selectID).value = selectCookieValue;

    //define variable to record the new value of the drop down.
    var selectValue = document.getElementById(selectID);
    //call the function to update cookie
    selectValue.onchange = updateCookie;
    }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    function getCookieValue(cookieName) {
    var thisCookie = document.cookie.split("; ");

    for (var i=0; i<thisCookie.length; i++) {
    if (cookieName == thisCookie[i].split("=")[0]) {
    return thisCookie[i].split("=")[1];
    }
    }
    return 0;
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    function updateCookie()
    {
    //get the new value
    updateValue = document.getElementById(selectID).value;
    //write the new entry to the cookie
    document.cookie = selectID+"Cookie="+updateValue;

    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    selectValue.onchange = function() {
        updateCookie(this.value);
    }
    function updateCookie(val)
    {
        document.cookie = selectID+"Cookie="+val;
    }


  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, thanks.

    i have changed the code to the below, but this is still only recording the last value

    window.onload = init;

    //declare selectID as a global variable
    var selectID = "";

    function init() {

    //create a variable of all the select tags - this method acts like an array in this instance hence the name
    var selectArray = document.getElementsByTagName('select');

    for(selectArray[count=0]; count < selectArray.length; count++)
    {
    //set selectID and selectCookie variables
    selectID = selectArray[count].id;
    var selectCookie = selectID+"Cookie";

    //get the value of the specified cookie
    var selectCookieValue = getCookieValue(selectCookie);
    //set the drop down to show the cookies value
    document.getElementById(selectID).value = selectCookieValue;

    //define variable to record the new value of the drop down.
    var selectValue = document.getElementById(selectID);
    //call the function to update cookie
    //selectValue.onchange = updateCookie;

    selectValue.onchange=function(){updateCookie(selectID);};


    }
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    function getCookieValue(cookieName) {
    var thisCookie = document.cookie.split("; ");

    for (var i=0; i<thisCookie.length; i++) {
    if (cookieName == thisCookie[i].split("=")[0]) {
    return thisCookie[i].split("=")[1];
    }
    }
    return 0;
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    function updateCookie(val){
    alert("hi"+val);
    updateValue = document.getElementById(val).value;
    document.cookie = val+"Cookie="+updateValue;}

    Last edited by paulonline2501; Aug 5, 2008 at 06:21.

  4. #4
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you add...

    alert("hi"+val);

    ...to updateCookie, you can see the problem.


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
  •