1

I'm using the following to do something when interacting with an input

$('input[name=hello]').on('input', function() {

...

});

Is it possible to trigger what's inside the function on load? I tried...

$('input[name=hello]').trigger();

Or

$('input[name=hello]').keypress();

...and it does not work. The purpose is to run on load whatever is inside teh input event without having to redeclare it.

6
  • What are you trying to listen for? Commented May 9, 2017 at 16:56
  • sounds similar: stackoverflow.com/questions/832059/… Commented May 9, 2017 at 16:56
  • You should be able to trigger it by $('input[name=hello]').trigger('input'); Commented May 9, 2017 at 16:57
  • Possible duplicate of Definitive way to trigger keypress events with jQuery Commented May 9, 2017 at 16:58
  • @MichaelFuller any interaction with the input Commented May 9, 2017 at 16:58

4 Answers 4

2

.trigger() takes any JavaScript event type. If you're listening for input, just trigger an input:

$('input[name=hello]').trigger('input');
Sign up to request clarification or add additional context in comments.

Comments

1

$(function(){

  function inputTriggered(e) {
    // May want to add a way to reduce excessive calls
    console.log('Triggered');
  }
  
  // Listen for a bunch of interactions
  $(document).on('click keydown blur focus', 'input', inputTriggered);

  // Trigger on load
  $('input[name=A]').trigger('click');

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<p>
<input name="A" value="A">
</p>

<p>
<input name="B" value="B">
</p>

Comments

1

I decide to elaborate on the comment I made before. How about separating the behaviour of the $('input[name=hello]').on('input', function() {...}); inside another function. Having done that, all you need is call the function on document ready function . In that manner you could also use that behaviour at your own leisure.

$(document).ready(function(){
    //The behaviour you desire
    function behaviour(){
         console.log('activated');
    }

    $('input[name=hello]').on('input', behaviour); //Bind your function to the event

    behaviour(); //Execute your function when the page loads
});

Comments

0

My method needs JQuery library :

<textarea></textarea>
<script>
  $(document).on('keydown', 'textarea', function (event) {
    if (event.key == 'Tab' || event.keyCode == 9) {
      event.preventDefault();
      var start = $(this)[0].selectionStart;
      var end = $(this)[0].selectionEnd;
      $(this)[0].value = $(this)[0].value.substring(0, start) + '\t' + $(this)[0].value.substring(end);
      $(this)[0].selectionStart = $(this)[0].selectionEnd = start + 1;
    }
  });
</script>

Comments

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.