Event.keyCode works once but not after on other objects











up vote
1
down vote

favorite












I'm currently attempting to make a sort of a chatting system, where first you can set your username and then send messages and both of the buttons, to set username and to send messages would trigger off of pressing enter, but for some reason it will not take the input for the messages, first it shows the username setting field



  <input id = "name" type = "text" name = "name" value = "" maxlength="20" placeholder = "Aseta käyttäjänimesi tähän">
<button id="user" type= "button" name="button" onclick = "setUsername()">


and this sets so enter makes it set it



   var input = document.getElementById("name");
var x = 0;
input.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("user").click();


after the username is set, the fields are replaced by these by using socket.io to check if the user is set:



  socket.on('userSet', function(data) {
user = data.username;
document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Kirjoita viestisi tähän">
<button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
<div id = "message-container"></div></div>';

});


after creating the new inputs, I have a piece of code that should make enter run the function to send messages which is:



  function sendMessage() {
var msg = document.getElementById('message').value;
if(msg) {
socket.emit('msg', {message: msg, user: user});
$('#message').val('');
}
}


and this should make so that enter sends the messages, but it does not work at all:



var inputtwo = document.getElementById("message");
inputtwo.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("sending").click();
alert("Toimii");
}
});


for no apparent reason the latter input listener doesn't work at all.










share|improve this question






















  • @Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
    – Veraen
    Nov 22 at 8:58










  • Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
    – Teemu
    Nov 22 at 9:00












  • I see I will fix that too
    – Veraen
    Nov 22 at 9:02










  • For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
    – Veraen
    Nov 22 at 9:24















up vote
1
down vote

favorite












I'm currently attempting to make a sort of a chatting system, where first you can set your username and then send messages and both of the buttons, to set username and to send messages would trigger off of pressing enter, but for some reason it will not take the input for the messages, first it shows the username setting field



  <input id = "name" type = "text" name = "name" value = "" maxlength="20" placeholder = "Aseta käyttäjänimesi tähän">
<button id="user" type= "button" name="button" onclick = "setUsername()">


and this sets so enter makes it set it



   var input = document.getElementById("name");
var x = 0;
input.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("user").click();


after the username is set, the fields are replaced by these by using socket.io to check if the user is set:



  socket.on('userSet', function(data) {
user = data.username;
document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Kirjoita viestisi tähän">
<button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
<div id = "message-container"></div></div>';

});


after creating the new inputs, I have a piece of code that should make enter run the function to send messages which is:



  function sendMessage() {
var msg = document.getElementById('message').value;
if(msg) {
socket.emit('msg', {message: msg, user: user});
$('#message').val('');
}
}


and this should make so that enter sends the messages, but it does not work at all:



var inputtwo = document.getElementById("message");
inputtwo.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("sending").click();
alert("Toimii");
}
});


for no apparent reason the latter input listener doesn't work at all.










share|improve this question






















  • @Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
    – Veraen
    Nov 22 at 8:58










  • Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
    – Teemu
    Nov 22 at 9:00












  • I see I will fix that too
    – Veraen
    Nov 22 at 9:02










  • For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
    – Veraen
    Nov 22 at 9:24













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm currently attempting to make a sort of a chatting system, where first you can set your username and then send messages and both of the buttons, to set username and to send messages would trigger off of pressing enter, but for some reason it will not take the input for the messages, first it shows the username setting field



  <input id = "name" type = "text" name = "name" value = "" maxlength="20" placeholder = "Aseta käyttäjänimesi tähän">
<button id="user" type= "button" name="button" onclick = "setUsername()">


and this sets so enter makes it set it



   var input = document.getElementById("name");
var x = 0;
input.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("user").click();


after the username is set, the fields are replaced by these by using socket.io to check if the user is set:



  socket.on('userSet', function(data) {
user = data.username;
document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Kirjoita viestisi tähän">
<button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
<div id = "message-container"></div></div>';

});


after creating the new inputs, I have a piece of code that should make enter run the function to send messages which is:



  function sendMessage() {
var msg = document.getElementById('message').value;
if(msg) {
socket.emit('msg', {message: msg, user: user});
$('#message').val('');
}
}


and this should make so that enter sends the messages, but it does not work at all:



var inputtwo = document.getElementById("message");
inputtwo.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("sending").click();
alert("Toimii");
}
});


for no apparent reason the latter input listener doesn't work at all.










share|improve this question













I'm currently attempting to make a sort of a chatting system, where first you can set your username and then send messages and both of the buttons, to set username and to send messages would trigger off of pressing enter, but for some reason it will not take the input for the messages, first it shows the username setting field



  <input id = "name" type = "text" name = "name" value = "" maxlength="20" placeholder = "Aseta käyttäjänimesi tähän">
<button id="user" type= "button" name="button" onclick = "setUsername()">


and this sets so enter makes it set it



   var input = document.getElementById("name");
var x = 0;
input.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("user").click();


after the username is set, the fields are replaced by these by using socket.io to check if the user is set:



  socket.on('userSet', function(data) {
user = data.username;
document.body.innerHTML = '<div class="wrapper"><div class="messagebox"><input type = "text" id = "message" placeholder = "Kirjoita viestisi tähän">
<button type = "button" id="sending" name = "button" onclick = "sendMessage()">Send</button></div>
<div id = "message-container"></div></div>';

});


after creating the new inputs, I have a piece of code that should make enter run the function to send messages which is:



  function sendMessage() {
var msg = document.getElementById('message').value;
if(msg) {
socket.emit('msg', {message: msg, user: user});
$('#message').val('');
}
}


and this should make so that enter sends the messages, but it does not work at all:



var inputtwo = document.getElementById("message");
inputtwo.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("sending").click();
alert("Toimii");
}
});


for no apparent reason the latter input listener doesn't work at all.







javascript jquery html socket.io






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 8:50









Veraen

3011




3011












  • @Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
    – Veraen
    Nov 22 at 8:58










  • Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
    – Teemu
    Nov 22 at 9:00












  • I see I will fix that too
    – Veraen
    Nov 22 at 9:02










  • For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
    – Veraen
    Nov 22 at 9:24


















  • @Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
    – Veraen
    Nov 22 at 8:58










  • Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
    – Teemu
    Nov 22 at 9:00












  • I see I will fix that too
    – Veraen
    Nov 22 at 9:02










  • For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
    – Veraen
    Nov 22 at 9:24
















@Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
– Veraen
Nov 22 at 8:58




@Teemu so I need to prevent default action and use key instead of keyCode, I will give it a try
– Veraen
Nov 22 at 8:58












Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
– Teemu
Nov 22 at 9:00






Umh ... you can't prevent the default action of keyup since that doesn't exist, you need to listen to keydown ... or the submit event itself.
– Teemu
Nov 22 at 9:00














I see I will fix that too
– Veraen
Nov 22 at 9:02




I see I will fix that too
– Veraen
Nov 22 at 9:02












For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
– Veraen
Nov 22 at 9:24




For now I haven't gotten it to work, any ideas on what to use instead of event.key or such actions? @Teemu
– Veraen
Nov 22 at 9:24












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Problem was solved by changing:



var inputtwo = document.getElementById("message");
inputtwo.addEventListener("keyup", function(event) {
if(event.keyCode === 13) {
document.getElementById("sending").click();
alert("Toimii");
}
});


into:



  var inputtwo = document.getElementById("message");
document.addEventListener("keypress", function(event) {
if (event.defaultPrevented) {
return;
}
var key = event.key || event.keyCode;
if( key === 'Enter' || key === 13) {
sendMessage();
}
});





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427006%2fevent-keycode-works-once-but-not-after-on-other-objects%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Problem was solved by changing:



    var inputtwo = document.getElementById("message");
    inputtwo.addEventListener("keyup", function(event) {
    if(event.keyCode === 13) {
    document.getElementById("sending").click();
    alert("Toimii");
    }
    });


    into:



      var inputtwo = document.getElementById("message");
    document.addEventListener("keypress", function(event) {
    if (event.defaultPrevented) {
    return;
    }
    var key = event.key || event.keyCode;
    if( key === 'Enter' || key === 13) {
    sendMessage();
    }
    });





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Problem was solved by changing:



      var inputtwo = document.getElementById("message");
      inputtwo.addEventListener("keyup", function(event) {
      if(event.keyCode === 13) {
      document.getElementById("sending").click();
      alert("Toimii");
      }
      });


      into:



        var inputtwo = document.getElementById("message");
      document.addEventListener("keypress", function(event) {
      if (event.defaultPrevented) {
      return;
      }
      var key = event.key || event.keyCode;
      if( key === 'Enter' || key === 13) {
      sendMessage();
      }
      });





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Problem was solved by changing:



        var inputtwo = document.getElementById("message");
        inputtwo.addEventListener("keyup", function(event) {
        if(event.keyCode === 13) {
        document.getElementById("sending").click();
        alert("Toimii");
        }
        });


        into:



          var inputtwo = document.getElementById("message");
        document.addEventListener("keypress", function(event) {
        if (event.defaultPrevented) {
        return;
        }
        var key = event.key || event.keyCode;
        if( key === 'Enter' || key === 13) {
        sendMessage();
        }
        });





        share|improve this answer












        Problem was solved by changing:



        var inputtwo = document.getElementById("message");
        inputtwo.addEventListener("keyup", function(event) {
        if(event.keyCode === 13) {
        document.getElementById("sending").click();
        alert("Toimii");
        }
        });


        into:



          var inputtwo = document.getElementById("message");
        document.addEventListener("keypress", function(event) {
        if (event.defaultPrevented) {
        return;
        }
        var key = event.key || event.keyCode;
        if( key === 'Enter' || key === 13) {
        sendMessage();
        }
        });






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 11:23









        Veraen

        3011




        3011






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427006%2fevent-keycode-works-once-but-not-after-on-other-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Berounka

            Fiat S.p.A.

            Type 'String' is not a subtype of type 'int' of 'index'