For example, I have two p tags and in each of them are two inputs (these input just take numbers). I want to add the numbers in each tag p on input and when that’s done, alert the highest number in these two p tags.
Example:
First
p: first input number(value) is:1, second input
is:2.
Secondp: first input:3, second input is:4.Adding the numbers in each tag
p:
Firstp:3&
Secondp:7I want to alert
7, because it is highest
number,
How do I do this by jQuery?
My try:(this doesn’t work)
<p>
<input type="text" value="1"/>
<input type="text" value="2"/>
</p>
<p>
<input type="text" value="3"/>
<input type="text" value="4"/>
</p>
$('button').live('click', function(){
var i = 0;
$('p').each(function(){
$('input',this).each(function(){
i += Number($(this).val());
//alert(val);
//if(val > i) {i = val; }
})
var val = Math.max.apply( Math, $(i).map( function(){
return 0|this.value;
}).get());
alert(val);
});
});
1 Answer