2

I'm trying to implement a messaging application in my game, so instead of clicking on the input text field manually, I want users to only press "enter", write something, then press "enter" again to submit.

For some reason, when I do this (press "enter"), the onclick alert fires from the input, but the input stays the same, I am still not able to type into the input form. If I manually click it, it works fine.

Am I missing something?

HTML

<form id="messageInput" action="">
    <input id="m" autocomplete="off" maxlength="100" onclick="alert('clicked')"/>
</form>

JAVASCRIPT

if(keyOn["enter"]){
    keyOn["enter"] = false;

    $('#m').click();

    console.log("clicked");
}
tery.blargh
  • 405
  • 1
  • 6
  • 23

5 Answers5

2

$('#m').click(function() {

  alert("click")
}).click();//click here to click automatically on load
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="messageInput" action="">
  <input id="m" autocomplete="off" maxlength="100" />
</form>

Using jquery you can call .click()

guradio
  • 15,524
  • 4
  • 36
  • 57
2

Thank you all for your help, but I somehow found the solution by randomly checking everything I saw:

$("#m").trigger("focus");

Use focus instead of click

tery.blargh
  • 405
  • 1
  • 6
  • 23
1

The code you've written is for capturing the click event ($("#m").click()).

Try:

$("#m").trigger("click");
Meet K.
  • 459
  • 3
  • 15
1

Try like this..press enter.Event will trigger.

if(confirm('Are you want submit message?')){
$("#m").keyup(function(event){
    if(event.keyCode == 13){
       alert('clicked');
       $("#m").val('');
    }
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="messageInput" action="">
  <input id="m" autocomplete="off" maxlength="100" />
</form>
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
0

I was having the same issue in an Angular app where the "click" would fire and open something from the element but it wouldn't do any of the angular method calls I had defined in (click)="...", I also found it wouldn't fire off anything defined in onclick()

The solution for me was to add .get(0)

$(el).get(0).click();
Jon
  • 54
  • 1
  • 5