I am currently working with a div
element that dynamically generates a table.
Although I can generate the table and use
on("click" or "mouseover", function)
to achieve certain results, I need the table to update as it loads.
In my initial attempts, I tried using each()
but realized it doesn't work on dynamic content. After further research, I switched to using on()
.
My goal is to avoid having to click on each cell for the changes to take effect.
Here's the jQuery code snippet:
$(function () {
$("#outputDiv2").on('click', "td", function () {
if ($(this).text() == 'notconnect') {
$(this).css('background-color', '#d5d5c3');
} else if ($(this).text() == 'connected') {
$(this).css('background-color', '#00e600');
} else if ($(this).text() == 'disabled') {
$(this).css('background-color', '#cc0000');
} else if ($(this).text() == 'err-disable') {
$(this).css('background-color', '#ff9900');
}
});
});
You can view an example HTML table and script on jsfiddle by clicking the link below: