I have a page with several total fields. They all share the "totalAmount" class.
I need to set the same value in all these fields with jquery:
var amt = some value;
$('.totamAmount').each // - Some instruction to set the value to amt;
To select elements by a given class name, you use the getElementsByClassName() method: let elements = document. getElementsByClassName('className');
Answer: Use the jQuery each() Method You can simply use the jQuery each() method to loop through elements with the same class and perform some action based on the specific condition.
The . class selector selects all elements with the specific class.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
Simply with val
:
$('.totamAmount').val('{the value you want here}');
If they're not form elements, use text:
$('.totamAmount').text('{the value you want here}');
If they are all input fields then you can just do this:
$(".totamAmount").val(amt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With