简体   繁体   中英

jQuery select input fields with the same placeholder value

I'm trying to select all input fields with the same placeholder-value. In this case "value1"

I can't edit the HTML due to the condition that I have to hack a PlugIn.

Here's my HTML:

 $('input[placeholder="value1"]').on('keyup', function() { $('.' + $(this).attr('placeholder')).val($(this).val()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Row 1: <input type="text" placeholder="value1"> <input type="text" placeholder="value1"> <br> Row 2: <input type="text" placeholder="value2"> <input type="text" placeholder="value3">

How do I select the placeholder-value?

what $('.' + $(this).attr('placeholder')) is doing is: Select all elements with the same class like the value of the placeholder attribute of the currently edited element.

Perhaps you're looking for $('[placeholder="' + $(this).attr('placeholder') + '"]') ? (Hopefully I got the quotation marks right)

In that case you want to read the placeholder of the current selected item and use that.

 $('input[placeholder="value1"]').on('keyup', function() { $('input[placeholder=' + $(this).attr('placeholder') + ']').val($(this).val()); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> Row 1: <input type="text" placeholder="value1"> <input type="text" placeholder="value1"> <br> Row 2: <input type="text" placeholder="value2"> <input type="text" placeholder="value3">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM