0

I have multiple input fields that have the same class name, such as:

<input type="text" class="input-name" value="John">
<input type="text" class="input-name" value="Maria">

When someone presses ENTER in any field, the text !!! is automatically appended to the value. This works great.

$(document).on('keypress', '.input-name', function(event)
{
    if (event.which != 13)
       return;
    
   $(this).val($(this).val() + '!!!');
});

The problem is that I'd like to press a button and change the value of all fields, by simulating the ENTER keypress event and for some reason, it only triggers in the first input!

This only triggers in the first input:

var e = $.Event('keypress', { which: 13 });
$('.input-name').trigger(e);

This also only triggers in the first input:

$('.input-name').each(function(i, input)
{
   $(input).trigger(e);
});

You can see the problem in this JSFiddle.

1
  • 2
    It's something to do with sharing the event amongst elements. Move the event creation into the button click event handler, and it works. EDIT: Sorry, move it into the each in the button click event handler so it is unique per element Commented Oct 29, 2020 at 17:38

3 Answers 3

2

Define jquery Event object inside the each function callback as follows.

$('.input-name').each(function (i, input) {
  var e = $.Event('keypress', {
    which: 13
  });
  $(input).trigger(e);
});
Sign up to request clarification or add additional context in comments.

Comments

1

I would just make a function that does the work and not deal with event handlers

//function alterInps(inps) {
//  inps.val(function () { return this.value + "!!!"; });
//}

function alterInps(inps) {
  var re = /!!!$/;
  inps.val(function () { 
    return this.value + (re.test(this.value) ? '' : '!!!');
  });
}

$(document).on('keypress', '.input-name', function(event) {
  if (event.which != 13) return;
  alterInps($(this));
});

$('button').on("click", function () {
  alterInps($('.input-name'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input-name" value="John">
<input type="text" class="input-name" value="Maria">
<button type="buton">All</button>

Comments

0

$('.input-name').on('keypress', function(event)
{
    if (event.which != 13)
    return;
    
   $(this).val($(this).val() + '!!!');
});

$(document).on('click', '#btn-submit', function()
{
    var e = $.Event('keypress', { which: 13 });
    //$('.input-name').trigger(e);
  
  $('.input-name').each(function(i, input)
  {
    $(input).trigger(e);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn-submit">
  Click me
</button>

<input type="text" class="input-name" value="Maria">
<input type="text" class="input-name" value="John">

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.