www.webdeveloper.com
Results 1 to 14 of 14

Thread: Changing input before KeyCode == 13 was entered

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    10

    Question Changing input before KeyCode == 13 was entered

    Hi,

    I am trying to re-assign a dom input element based on it's content after hitting the [enter] key.
    thing is...the [enter] is executed ahead of the conditions. Confirm dialog works fine but I don't want to use any dialogs.

    It's part of a chrome extension...

    Here is my code:

    document.onkeyup = returnKey;

    function returnKey(evt)
    {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if ( node.value == 'fi' ) { node.value = "כן"; }
    if ( node.value == 'kt' ) { node.value = "לא"; }

    }

    I want to change the value of node.value,
    only after that, [enter] should be executed. ( with updated value ).

    Thanks,
    Asaph.

  2. #2
    Join Date
    Dec 2012
    Posts
    10
    conditions should look like that ( just to show you the basic logic of what I want to achieve. )

    if ( evt.keyCode == 13 )
    {
    if ( node.value == 'fi' ) { evt.srcElement.value = "כן"; }
    if ( node.value == 'kt' ) { evt.srcElement.value = "לא"; }
    }

    but it's not working.

    thanks

  3. #3
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Pressing Enter in a text field causes form submission, so you must cancel submission by returning false if applicable.

    The keyup event is too late to be of use:

    Code:
    <script type='text/javascript'>
    
    document.onkeypress = returnKey;
    
    function returnKey( e )
    {
      var evt = e || window.event,
          node = evt.srcElement || evt.target,
          query = false;
    
      if ( evt.keyCode == '13' && ( query = /^(fi|kt)$/.test( node.value ) ) )
        node.value = "??";
    
      return !query;
    }
    
    </script>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  4. #4
    Join Date
    Dec 2012
    Posts
    10
    well, thanks but that's not it

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    What do you mean "that's not it"?

    Are you saying I got the spec wrong or that you can't get it to work?
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  6. #6
    Join Date
    Dec 2012
    Posts
    10
    The code is implemented, but it's not doing what it should.

  7. #7
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by assaf.vilmovski View Post
    The code is implemented, but it's not doing what it should.
    That answers neither question.

    Does this demo do what you expected?
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  8. #8
    Join Date
    Dec 2012
    Posts
    10
    It does.

    I`m gonna take another look at this later.

    Thank you very much for your time.

  9. #9
    Join Date
    Dec 2012
    Posts
    10
    It does.
    It just didn't work on the extension.
    I`m gonna take another look at this later.

    Thank you very much for your time.

    by the way, is there a more human reabable way of writing this?
    You know, to simple things out

  10. #10
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by assaf.vilmovski View Post
    is there a more human reabable way of writing this?
    You know, to simple things out
    Simplifying it would make it more complicated, and I'm sure your teacher will understand it as it is.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  11. #11
    Join Date
    Dec 2012
    Posts
    10
    well now you're insulting me...

  12. #12
    Join Date
    Dec 2012
    Posts
    10
    How can I insert my code before their's?
    Any ides?

  13. #13
    Join Date
    Dec 2012
    Posts
    10
    One thought is using innerHTML to rewrite the targeted website's source.

  14. #14
    Join Date
    Dec 2012
    Posts
    10
    now I see the real problem...
    the website that the extension is working on is already using an onkeydown on the input.
    So it causes their code to be executed before mine...
    In all other websites it's working great.

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