jQuery set value of td cell depending on values of other cells using Array and if/else if












0















I've Run into another problem trying to show different SLA statuses in the slaClass td.



I was previously using console.log("red") etc to show the statuses for testing. This worked perfectly, however when then trying to set the text for the slaClass field it just shows Warning and not the correct values.






$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Demo code - http://jsfiddle.net/ZmesE/294/










share|improve this question




















  • 1





    Does your demo code not work in a StackOverflow snippet?

    – connexo
    Nov 21 '18 at 15:57
















0















I've Run into another problem trying to show different SLA statuses in the slaClass td.



I was previously using console.log("red") etc to show the statuses for testing. This worked perfectly, however when then trying to set the text for the slaClass field it just shows Warning and not the correct values.






$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Demo code - http://jsfiddle.net/ZmesE/294/










share|improve this question




















  • 1





    Does your demo code not work in a StackOverflow snippet?

    – connexo
    Nov 21 '18 at 15:57














0












0








0








I've Run into another problem trying to show different SLA statuses in the slaClass td.



I was previously using console.log("red") etc to show the statuses for testing. This worked perfectly, however when then trying to set the text for the slaClass field it just shows Warning and not the correct values.






$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Demo code - http://jsfiddle.net/ZmesE/294/










share|improve this question
















I've Run into another problem trying to show different SLA statuses in the slaClass td.



I was previously using console.log("red") etc to show the statuses for testing. This worked perfectly, however when then trying to set the text for the slaClass field it just shows Warning and not the correct values.






$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Demo code - http://jsfiddle.net/ZmesE/294/






$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





$(document).ready(function() {
var daysLeft = $('.DaysLeft');
var sla = $('.slaClass');
$(daysLeft).each(function() {
var daysLeft = $(this).text(); // assign a current value to increase its performance.

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>






jquery html arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:00









Rory McCrossan

245k29212248




245k29212248










asked Nov 21 '18 at 15:54









WHowardWHoward

518




518








  • 1





    Does your demo code not work in a StackOverflow snippet?

    – connexo
    Nov 21 '18 at 15:57














  • 1





    Does your demo code not work in a StackOverflow snippet?

    – connexo
    Nov 21 '18 at 15:57








1




1





Does your demo code not work in a StackOverflow snippet?

– connexo
Nov 21 '18 at 15:57





Does your demo code not work in a StackOverflow snippet?

– connexo
Nov 21 '18 at 15:57












2 Answers
2






active

oldest

votes


















2














The problem is that you are not grabbing the sibling ".slaClass" element from your current ".daysLeft" element.



Try this:



        $(document).ready(function(){
var daysLeft = $('.DaysLeft');

$(daysLeft).each(function() {
var daysLeft=$(this).text(); // assign a current value to increase its performance.
var sla = $(this).siblings('.slaClass');
if(daysLeft !="" && daysLeft!=undefined)
{
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
}else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
}else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
}
else(alert("Error"));
})
});





share|improve this answer
























  • Looks good to me

    – Jamie_D
    Nov 21 '18 at 16:05



















2














The issue is because you're updating all the .slaClass elements instead of the one related to the current .DaysLeft element in the each iteration. To fix this, use closest() to get the parent tr of the current element. Try this:






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Also note that in your original logic DaysLeft and slaClass are already jQuery objects, so you don't need to put them in jQuery objects again.



It's also not a good idea to re-use the same variable name, even if it is in a different scope. That could very easily lead to some unnecessary confusion.






share|improve this answer


























  • May I be allowed to add my opinion to your answer? I suggest to use .next()

    – Foo
    Nov 21 '18 at 16:05






  • 1





    @TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

    – Rory McCrossan
    Nov 21 '18 at 16:07











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415869%2fjquery-set-value-of-td-cell-depending-on-values-of-other-cells-using-array-and-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The problem is that you are not grabbing the sibling ".slaClass" element from your current ".daysLeft" element.



Try this:



        $(document).ready(function(){
var daysLeft = $('.DaysLeft');

$(daysLeft).each(function() {
var daysLeft=$(this).text(); // assign a current value to increase its performance.
var sla = $(this).siblings('.slaClass');
if(daysLeft !="" && daysLeft!=undefined)
{
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
}else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
}else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
}
else(alert("Error"));
})
});





share|improve this answer
























  • Looks good to me

    – Jamie_D
    Nov 21 '18 at 16:05
















2














The problem is that you are not grabbing the sibling ".slaClass" element from your current ".daysLeft" element.



Try this:



        $(document).ready(function(){
var daysLeft = $('.DaysLeft');

$(daysLeft).each(function() {
var daysLeft=$(this).text(); // assign a current value to increase its performance.
var sla = $(this).siblings('.slaClass');
if(daysLeft !="" && daysLeft!=undefined)
{
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
}else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
}else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
}
else(alert("Error"));
})
});





share|improve this answer
























  • Looks good to me

    – Jamie_D
    Nov 21 '18 at 16:05














2












2








2







The problem is that you are not grabbing the sibling ".slaClass" element from your current ".daysLeft" element.



Try this:



        $(document).ready(function(){
var daysLeft = $('.DaysLeft');

$(daysLeft).each(function() {
var daysLeft=$(this).text(); // assign a current value to increase its performance.
var sla = $(this).siblings('.slaClass');
if(daysLeft !="" && daysLeft!=undefined)
{
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
}else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
}else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
}
else(alert("Error"));
})
});





share|improve this answer













The problem is that you are not grabbing the sibling ".slaClass" element from your current ".daysLeft" element.



Try this:



        $(document).ready(function(){
var daysLeft = $('.DaysLeft');

$(daysLeft).each(function() {
var daysLeft=$(this).text(); // assign a current value to increase its performance.
var sla = $(this).siblings('.slaClass');
if(daysLeft !="" && daysLeft!=undefined)
{
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$(sla).text('Red');
}else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$(sla).text('Warning');
}else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$(sla).text('Green');
}
}
else(alert("Error"));
})
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 16:00









MikeSMikeS

1,5441510




1,5441510













  • Looks good to me

    – Jamie_D
    Nov 21 '18 at 16:05



















  • Looks good to me

    – Jamie_D
    Nov 21 '18 at 16:05

















Looks good to me

– Jamie_D
Nov 21 '18 at 16:05





Looks good to me

– Jamie_D
Nov 21 '18 at 16:05













2














The issue is because you're updating all the .slaClass elements instead of the one related to the current .DaysLeft element in the each iteration. To fix this, use closest() to get the parent tr of the current element. Try this:






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Also note that in your original logic DaysLeft and slaClass are already jQuery objects, so you don't need to put them in jQuery objects again.



It's also not a good idea to re-use the same variable name, even if it is in a different scope. That could very easily lead to some unnecessary confusion.






share|improve this answer


























  • May I be allowed to add my opinion to your answer? I suggest to use .next()

    – Foo
    Nov 21 '18 at 16:05






  • 1





    @TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

    – Rory McCrossan
    Nov 21 '18 at 16:07
















2














The issue is because you're updating all the .slaClass elements instead of the one related to the current .DaysLeft element in the each iteration. To fix this, use closest() to get the parent tr of the current element. Try this:






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Also note that in your original logic DaysLeft and slaClass are already jQuery objects, so you don't need to put them in jQuery objects again.



It's also not a good idea to re-use the same variable name, even if it is in a different scope. That could very easily lead to some unnecessary confusion.






share|improve this answer


























  • May I be allowed to add my opinion to your answer? I suggest to use .next()

    – Foo
    Nov 21 '18 at 16:05






  • 1





    @TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

    – Rory McCrossan
    Nov 21 '18 at 16:07














2












2








2







The issue is because you're updating all the .slaClass elements instead of the one related to the current .DaysLeft element in the each iteration. To fix this, use closest() to get the parent tr of the current element. Try this:






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Also note that in your original logic DaysLeft and slaClass are already jQuery objects, so you don't need to put them in jQuery objects again.



It's also not a good idea to re-use the same variable name, even if it is in a different scope. That could very easily lead to some unnecessary confusion.






share|improve this answer















The issue is because you're updating all the .slaClass elements instead of the one related to the current .DaysLeft element in the each iteration. To fix this, use closest() to get the parent tr of the current element. Try this:






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





Also note that in your original logic DaysLeft and slaClass are already jQuery objects, so you don't need to put them in jQuery objects again.



It's also not a good idea to re-use the same variable name, even if it is in a different scope. That could very easily lead to some unnecessary confusion.






$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>





$(document).ready(function() {
$('.DaysLeft').each(function() {
var $sla = $(this).closest('tr').find('.slaClass');
var daysLeft = $(this).text();

if (daysLeft != "" && daysLeft != undefined) {
if (parseInt(daysLeft) <= 30) {
console.log('Red');
$sla.text('Red');
} else if (parseInt(daysLeft) > 30 && parseInt(daysLeft) < 90) {
console.log('Warning');
$sla.text('Warning');
} else if (parseInt(daysLeft) >= 90) {
console.log('Green');
$sla.text('Green');
}
} else(alert("Error"));
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<th>Applicants</th>
<th>Interviews</th>
<th>Offers</th>
<th>Days Left to Fill </th>
<th>SLA</th>
</tr>
<tr>
<td>530</td>
<td>50</td>
<td>1</td>
<td class="DaysLeft">125</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>56</td>
<td>0</td>
<td>0</td>
<td class="DaysLeft">25</td>
<td class="slaClass"></td>
</tr>
<tr>
<td>82</td>
<td>6</td>
<td>0</td>
<td class="DaysLeft">62</td>
<td class="slaClass"></td>
</tr>
</tbody>
</table>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 16:05

























answered Nov 21 '18 at 16:02









Rory McCrossanRory McCrossan

245k29212248




245k29212248













  • May I be allowed to add my opinion to your answer? I suggest to use .next()

    – Foo
    Nov 21 '18 at 16:05






  • 1





    @TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

    – Rory McCrossan
    Nov 21 '18 at 16:07



















  • May I be allowed to add my opinion to your answer? I suggest to use .next()

    – Foo
    Nov 21 '18 at 16:05






  • 1





    @TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

    – Rory McCrossan
    Nov 21 '18 at 16:07

















May I be allowed to add my opinion to your answer? I suggest to use .next()

– Foo
Nov 21 '18 at 16:05





May I be allowed to add my opinion to your answer? I suggest to use .next()

– Foo
Nov 21 '18 at 16:05




1




1





@TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

– Rory McCrossan
Nov 21 '18 at 16:07





@TânNguyễn that's an alternative, but I prefer the use of closest() and find() as it's more robust. If a designer/HTML editor changes the order of the fields, or adds a new one in between, this logic will still work. Using next() would break. Perhaps not important when working alone or in a small team, but in enterprise level apps with a large distributed team, it can be a big problem.

– Rory McCrossan
Nov 21 '18 at 16:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415869%2fjquery-set-value-of-td-cell-depending-on-values-of-other-cells-using-array-and-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory