If the number of element B is smaller than the number of element A, I want to make a decision on element C.

Asked 1 years ago, Updated 1 years ago, 352 views

"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

javascript query

2023-01-04 22:47

1 Answers

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>


2023-01-04 23:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.