Jump to content

add button click when enter key on input text


nitiphone2021

Recommended Posts

Dear All,

from begin I have a Button, when user click this button, on javascript will create a input text name and button submit show on dialog of jquery like this

 var modal_content = '<div id="user_dialog_' + to_user_id + '" class="user_dialog" title="' + office + '">';

        modal_content += '<div style="height:310px;  overflow-y: scroll; margin:0px; padding:0px;" class="chat_history " data-touserid="' + to_user_id + '" id="chat_history_' + to_user_id + '">';

        modal_content += '</div>';

        modal_content += '<div class="form-group">';

        modal_content += '</div><div class="form-group" align="right">';
        modal_content += '</div>';
        modal_content += '<hr style="margin:10px;padding:0">';
        modal_content += '<div class="input-group">';
        modal_content += '<input type="text" name="chat_message_' + to_user_id + '" id="chat_message_' + to_user_id + '" placeholder="ພິມຂໍ້ຄວາມຢູ່ບ່ອນນີ້" class="form-control chat_message" style="border: none;">';
        modal_content += '<div class="input-group-append">';
        modal_content += '<span class="input-group-text" style="background-color: transparent !important;border: none;padding:0"><div class="filediv"><input type="file" id="file"><label for="file-7"><strong><i class="fa fa-file fa-lg ml-1" aria-hidden="true"></i></strong></label></div><i name="send_chat" id="' + to_user_id + '"  class="fa fa-paper-plane fa-lg pl-1 mb-1 send_chat" aria-hidden="true"></i></span>';
        modal_content += '</div>';
        modal_content += '</div><span id="attachedfile"></span><script>$("#chat_message_' + to_user_id + '").keyup(function(event) {if (event.keyCode === 13) {$("#' + to_user_id + '").click();}});</script>';
      
        $('#user_model_details').html(modal_content);

so I tried to create a script as below to get enter key of input text name then make like user click on the button.

If I tried to double enter  then I will get below error message

I don't know why it's not work. Do you have any idea to fix it?

double enter key.png

Link to comment
Share on other sites

You shouldn't have multiple elements with the same ID. In your case, your input is being duplicated and the id is "chat_message_{to_user_id}", so when there are multiple instances of the input element they're all assigned the same id - in this case, "chant_message_13".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.