Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery set all elements in a given class the same html

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;
like image 635
Didier Levy Avatar asked Apr 29 '13 20:04

Didier Levy


People also ask

How do you select all elements with the same class?

To select elements by a given class name, you use the getElementsByClassName() method: let elements = document. getElementsByClassName('className');

How do you loop through an element with the same class in jQuery?

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.

Which jQuery statement can you use to select all the elements on a webpage that has the CSS class name?

The . class selector selects all elements with the specific class.

How do you target a class in jQuery?

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.


2 Answers

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}');
like image 174
gdoron is supporting Monica Avatar answered Nov 15 '22 09:11

gdoron is supporting Monica


If they are all input fields then you can just do this:

$(".totamAmount").val(amt);
like image 36
Lars Juel Jensen Avatar answered Nov 15 '22 11:11

Lars Juel Jensen