"If the following budget (class="budget") exceeds the cost (class="cost"), I would like to submit a decision (class="result") as ""over-budget"" in the decision (class re"result"), but it doesn't work."
I look forward to your kind cooperation.
▼ HTML
<div>
<div>span class="budget">100</span>span class="cost">80</span>>span class="result">/span>>>
<div>span class="budget">50</span>span class="cost">70</span>>span class="result">/span>>>
<div><span class="budget">30</span class="cost">50>/span class="result">/span class ""result">>/span>>
</div>
▼Tried code
<script>
varbudget=$('.budget').text();
varcost=$('.cost') .text();
if(budget<cost){
$('.result') .text('over budget');
}
</script>
↓↓↓
▼ Ideal results
10080
5070 Over budget
3050 Budget Over
▼ Actual Results
10080 Budget Over
5070 Over budget
3050 Budget Over
There are multiple elements, so store them in an array and compare them at the same index.
<script>
const budget=$('.budget') .map(_,e)=>Number(e.innerText)) .get();
const cost = $('.cost').map((_,e)=>Number(e.innerText))).get();
$('.result') .each(i,e)=>{
if(budget[i]<cost[i]){
e.innerText = 'Over budget';
}
});
</script>
© 2024 OneMinuteCode. All rights reserved.