I'm not able to copy an Input Field value to multiple fields using Javascript

I have been working on this system for a while now, and I've run into a small problem.

What I want to do: Copy all the data entered in an input field with id 'totalentered' into all the input fields with id 'totalclasses'

What is happening instead: There are multiple fields with the id 'totalclasses', generated using a while loop in PHP. However, the data is only being copied to the first input field with id 'totalclasses', rest of them remaining unchanged.

I'm just not able to figure out what's wrong. Here's my HTML+PHP code

<input type="text" class="form-control" placeholder="Total Classes Taken" id="totalentered">
<!--PHP Code, deleted because it would make reading this post too tiresome-->
<tr>
<th scope="row"><?=$row['rollnumber']?></th>
<td><?=$row['name']?></td>
<td><?php echo $selectedmonth; ?></td>
<td><input data-parsley-type="digits" type="text" class="form-control" placeholder="Classes Attended" name="attended<?= $cnt; ?>" id="attended"></td>
<td><input data-parsley-type="digits" type="text" class="form-control" placeholder="Total Classes Taken" id="totalclasses" name="totalclasses<?= $cnt; ?>"></td>
<input type="hidden" name="student_id<?= $cnt; ?>" value="<?=$student_id?>">
<input type="hidden" name="testmarks<?= $cnt; ?>" value="0">
<input type="hidden" name="assignmentmarks<?= $cnt; ?>" value="0">
<input type="hidden" name="submitted" value="true">

Here's my JavaScript code which should be doing the job:

<script type="text/javascript">
        $(document).ready(function() {
            $('form').parsley();
        });
        $(function() {                                       // <== Doc Ready
            $("#totalentered").change(function() {           // When the total number of classes are changed
                $('#totalclasses').val(this.value);          // copy it over to the individual student for convenience
            });
        });
    </script>

Anyone who can help me out with what's wrong? Thanks in advance!

I also agree that you should not use more than one id with the same name in your HTML. The great thing about jquery though is that you can use a number of ways to lookup an element. For instance:

<input myInput="great">Some text</a>
<script>
$( "input[myInput|='great']" ).val(this.value); //Search for an attribute name value pair
$( "input[myInput]" ).val(this.value); //Just search for an attribute name
$( "input[myInput*='eat']" ).val(this.value ); //star based search
</script>

You can see a full list of all available css selectors here:

https://api.jquery.com/category/selectors/

Kendrick Lamar - i (Audio), Log in to i-Ready®, online assessment and instruction that helps teachers provide all students a path to proficiency and growth in reading and mathematics. Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family.

You should not have multiple elements with the same id. In your PHP code, change the totalclasses into a class attribute.

Then replace

$('#totalclasses').val(this.value);

with

$('.totalclasses').val(this.value);

From the jQuery docs:

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

I (Kendrick Lamar song), I definition, the ninth letter of the English alphabet, a vowel. See more. I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I?

Write id in input field as id="totalclasses"

<script type="text/javascript">
    $(document).ready(function() {
        $('form').parsley();
    });
    $(function() {                                       
        $("#totalentered").change(function() { 
            var cnt = "<?php echo $cnt ?>";
            $('#totalclasses' + cnt).val(this.value);         
        });
    });
</script>

Log in to i-Ready, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. Unsubscribe from Kendrick Lamar? Want to watch this again later? Sign in to add this video to a playlist. Need to report the video? Sign in to report inappropriate content. Sign in to make your

I, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? Translingual: ·The letter i with an acute accent.··water Definition from Wiktionary, the free dictionary

I, I-94 is a place for U.S. visitors to find travel records. Î, î (i-circumflex) is a letter in the Friulian, Kurdish, Old Malay and Romanian alphabets.This letter also appears in French, Turkish, Italian, Welsh and Walloon languages as a variant of letter “i”.. Afrikaans. In Afrikaans, î is a punctuated form of i: wîe, the plural of wig ('wedge').

I, www.will.i.am is the home of everything will.i.am, the multi-faceted entertainer and creative innovator, and a seven-time Grammy Award winner. Welcome back to Instagram. Sign in to check out what your friends, family & interests have been capturing & sharing around the world.

Comments
  • There are multiple fields with the id 'totalclasses' - The id attribute must be unique, so you will need to change this.
  • Any idea what alterations I can make, to get the desired result? I need it to be replicated into all those fields which are being generated...
  • Use class instead ... Id will work only for first element
  • Also, I forgot to mention, it was working when I originally wrote this code for multiple fields with the same ID, for some unknown reason.
  • @AshishKumar the class is same even for fields which aren't supposed to have that data, for styling purposes - that's the reason I didn't use class
  • Thanks - this was perfect.
  • Class is same even for fields which aren't supposed to have that data, for styling purposes - that's the reason I didn't use class...