I am trying to conditionally remove a class from an HTML string that is built within a JS function.
Note that in the live project, the values in the table and the value in the rows variable change based on an AJAX call. Here, using hard coded values is sufficient to demonstrate this issue.
Why isn't the colorful class being removed?
var rows = 2;
var table = "<table class=\"colorful\"><thead><tr>column 1</tr></thead><tbody><tr><td>A</td></tr><tr><td>B</td></tr></tbody></table>";
if (rows <= 2) {
$(".colorful", $(table).context).removeClass("colorful");
}
$("body").append(table);
.colorful {
color: red;
font-size: x-large;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>