0

I created a script in JavaScript to take value from one input and place it in several inputs, but I have a problem that value is only put in the first input.

The JavaScript code is:

jQuery(document).ready(function ($) {

var $apartament = $('#value-apartament'),

$permetru = $('#put-value');

$apartament.on('input', function () {

$permetru.val($apartament.val());

});

});

The first input have the id: #value-apartament and the others have #put-value for copy the value from the first input and put for the rest of three.

image: https://prnt.sc/nyj_S9AjhaMW

Emre
  • 723
  • 6
  • 14
IND
  • 1
  • 2

3 Answers3

0

Firstly, the # id attribute is used for unique elements. It is better to use . class attribute to select multiple input with the class of .put-value.

$('input#value-apartament').change(function() {
    $('input.put-value').val($(this).val());
});
0

Ids should be used to identify a unique element of the DOM, so using #value-apartment is correct for the first input. But having the same id #put-value for the other three is wrong.

You should use a class for those 3 inputs, and you can select those using ($'.put-value')

0

An ID should exist only once in the whole HTML document - use classes if you want to assign it to multiple elements. As answered in this question, jQuery will return the first element in the document with the id, and that is what happened for you.

OuterSoda
  • 175
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '22 at 19:59