Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  159] [ 7]  / answers: 1 / hits: 18047  / 14 Years ago, mon, january 17, 2011, 12:00:00

How can I use these JavaScript math functions ?



For example, I want to compute the square of all <input> values in a form, without submiting the form.



Can you give a little example? Thank you.


More From » jquery

 Answers
92

You can act on each individual input using an each()(docs) loop.



Click here to test a working example. (jsFiddle)



$('a.square').click(function() {
$('#myform :text').each(function() {
this.value *= this.value;
});
});

$('a.square_root').click(function() {
$('#myform :text').each(function() {
this.value = Math.sqrt(this.value);
});
});


When either link is clicked, it finds all the text inputs in myform and iterates over them.



Inside the each function, this refers to the current input element.


[#94187] Saturday, January 15, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
whitney

Total Points: 642
Total Questions: 110
Total Answers: 98

Location: Solomon Islands
Member since Mon, Jun 20, 2022
2 Years ago
;