1

I have one function for send message if the Enter is pressed. But, I created one button for send message too, and if I call this function, the message does not send, and I added the click event.

  function inputKeyDown(event, inputBox) {
    // Submit on enter key, dis-allowing blank messages
    if (event.keyCode === 13 && inputBox.value || event.onclick && inputBox.value) {            

      // Send the user message
      Api.sendRequest(inputBox.value, context);

      // Clear input box for further messages
      inputBox.value = '';
      Common.fireEvent(inputBox, 'input');
    }
  }

I try add one click event inside this if condition and does not work, I test the button with one alert event and works fine when I click.

Check my html:

<label for="textInput" class="inputOutline">
          <input id="textInput" class="input responsive-column"
           placeholder="Type something" type="text"
           onkeydown="ConversationPanel.inputKeyDown(event, this)">

            <button class="Testbutton" onclick="ConversationPanel.inputKeyDown(event, this)">
              <i class="icon send"></i>
            </button>

 </label>

My function onkeydown works fine to send the message. But when I changed the function added one onclick event, my button doesn't work. I debugged the button with alert and work perfectly.

What am I doing wrong? Someone can help me please.

5
  • What is Common? Commented May 19, 2017 at 18:19
  • Is for send my message inside the input element. I'm using some API. My event to send works perfectly when I press enter. I just want send if user click in the button too. Commented May 19, 2017 at 18:21
  • what was your onclick=? Commented May 19, 2017 at 18:22
  • <button class="Testbutton" onclick="alert('test')"> I try use the same function but does not work =\ Commented May 19, 2017 at 18:23
  • if by same function you mean ConversationPanel.inputKeyDown(event, this) then of course it won't work, because you need to pass input (2nd param) Commented May 19, 2017 at 18:25

2 Answers 2

2

create a new function sendMessage then call it from different events like below.

var inputBox = document.getElementById('textInput');

// for input keydown
function inputKeyDown(event) {
  // Submit on enter key, dis-allowing blank messages
  if (event.keyCode === 13) {
    sendMessage();
  }
}

// for button click
function sendMessage() {
  if (inputBox.value) {
    // Send the user message
    console.log(inputBox.value);
    Api.sendRequest(inputBox.value, context);

    // Clear input box for further messages
    inputBox.value = '';
    Common.fireEvent(inputBox, 'input');
  }
}
<label for="textInput" class="inputOutline">
  <input id="textInput" class="input responsive-column"
   placeholder="Type something" type="text"
   onkeydown="inputKeyDown(event)">
  <button class="Testbutton" onclick="sendMessage()">
    <i class="icon send"></i>Send
  </button>
</label>

Sign up to request clarification or add additional context in comments.

1 Comment

Hey man, your example works too, but, in this case I needed use just the function inputKeyDown() because other business rules in my application. Thanks too! +1
1

The if condition needs adjustment. In order to check if the event was a click, you need to use event.type === 'click'. Also it can be simplified a bit by checking inputBox.value just once. I modified your inputKeyDown function:

function inputKeyDown(event, inputBox) {
    // Submit on enter key, dis-allowing blank messages
    if (inputBox.value && event.keyCode === 13 || event.type === 'click') {            

      // Send the user message
      Api.sendRequest(inputBox.value, context);

      // Clear input box for further messages
      inputBox.value = '';
      Common.fireEvent(inputBox, 'input');
    }
  }

Also, you need to pass the input element as the second parameter on the onclick handler:

<label for="textInput" class="inputOutline">
          <input id="textInput" class="input responsive-column"
           placeholder="Type something" type="text"
           onkeydown="ConversationPanel.inputKeyDown(event, this)">

            <button class="Testbutton" onclick="ConversationPanel.inputKeyDown(event, document.getElementById('textInput'))">
              <i class="icon send"></i>
            </button>

 </label>

1 Comment

Works perfectly! Thank u!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.