onKeyPress problem

Discussion in 'JavaScript' started by redhits, Oct 9, 2009.

  1. #1
    So this is my problem :)

    I am having an input type , with a default value="monkey"


    now... i added onKeyPress ... and i am calling a function called detector() ...

    the problem is this... if somebody press 1... the text will be money1...
    but if i am doing

    function detector(){
    newurl=document.getElementById("url").value;
    document.write(newurl);
    }



    it will write monkey, even if the last key pressed was 1 and the new text was money1 ...
     
    redhits, Oct 9, 2009 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    try onkeypress -> onkeydown / onkeyup, the latter one probably being raised AFTER the concatenation of the string.
     
    dimitar christoff, Oct 10, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Will "detector(); return false;" solve your problem ???
     
    camjohnson95, Oct 10, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    watch it in action onkeyup (via mootools) here:
    http://mooshell.net/YBPBb/

    just type in the input field. there is no need to stop the event propagation.

    the second field is on keypress which is BEFORE the event propagation, it confirms your problem.
     
    Last edited: Oct 10, 2009
    dimitar christoff, Oct 10, 2009 IP