jQuery set value of td cell depending on values of other cells using Array and if/else if
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/
jquery html arrays
add a comment |
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/
jquery html arrays
1
Does your demo code not work in a StackOverflow snippet?
– connexo
Nov 21 '18 at 15:57
add a comment |
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/
jquery html arrays
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
jquery html arrays
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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"));
})
});
Looks good to me
– Jamie_D
Nov 21 '18 at 16:05
add a comment |
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.
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 ofclosest()
andfind()
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. Usingnext()
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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"));
})
});
Looks good to me
– Jamie_D
Nov 21 '18 at 16:05
add a comment |
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"));
})
});
Looks good to me
– Jamie_D
Nov 21 '18 at 16:05
add a comment |
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"));
})
});
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"));
})
});
answered Nov 21 '18 at 16:00
MikeSMikeS
1,5441510
1,5441510
Looks good to me
– Jamie_D
Nov 21 '18 at 16:05
add a comment |
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
add a comment |
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.
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 ofclosest()
andfind()
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. Usingnext()
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
add a comment |
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.
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 ofclosest()
andfind()
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. Usingnext()
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
add a comment |
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.
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>
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 ofclosest()
andfind()
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. Usingnext()
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
add a comment |
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 ofclosest()
andfind()
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. Usingnext()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Does your demo code not work in a StackOverflow snippet?
– connexo
Nov 21 '18 at 15:57