www.webdeveloper.com
Results 1 to 8 of 8

Thread: Add onChange to HTML using Javascript?

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Add onChange to HTML using Javascript?

    hi everyone,

    I am working on web page where I only have access to add JavaScript and I need to grab real-time text that is typed into a text field and transfer it to a separate page containing another form. One way I thought about doing this was creating a cookie on the first form and saving the values, then pulling values from the second form. The problem I encounter is that I cannot modify the <input type=text name="First__bName" class=ticketField id="userfield6" >, so I was wondering if there was a way to add onChange(this.name) using JavaScript in order to save the text into the cookie. Thanks!

    -Pen

  2. #2
    Join Date
    Oct 2010
    Posts
    36
    you could try this:
    Code:
    document.getElementById("userfield6").onchange=function(){
    //Eat the cookies, whoops, do the cookie thing...(hungry)
    }

  3. #3
    Join Date
    Oct 2010
    Posts
    5
    thanks for the reply! so the cookie function i am using to set variables is
    Code:
    function setCookie(name, value, days){ //set cookie value
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring=expireDate.setDate(expireDate.getDate()+parseInt(days))
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
    }
    I tried just setting the values inside but that doesnt seem to be working, i.e.

    Code:
    document.getElementById("userfield6").onchange=function(){
    //Eat the cookies, whoops, do the cookie thing...(hungry)
    setCookie("test",this.fName, 30)
    }

  4. #4
    Join Date
    Oct 2010
    Posts
    36
    I think the problem is in this.fName
    Are you trying to read what the user typed in "userfield6"?
    Try using this.value.

  5. #5
    Join Date
    Oct 2010
    Posts
    5
    I just tested by trying to get the function to print random text whenever it was triggered however nothing shows so I don't think it is triggering properly

  6. #6
    Join Date
    Oct 2010
    Posts
    36
    really? try pressing enter or selecting other element. The event isn't fired everytime the user presses a key, but the time the user loses focus on the field, or at least that's what it seems...

  7. #7
    Join Date
    Oct 2010
    Posts
    5
    Yea i thought it worked that way also...I guess I'll experiment a bit more with this

  8. #8
    Join Date
    Oct 2010
    Posts
    36
    yea if it worked the other way it wouldn't be wise because (for ex) the script would create a cookie everytime the user pressed a key. Experimenting is always fun!!

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