Background
If users have the item called Get All X
in their cart, then I want to hide the upsell section called wcf-bump-order-content
.
What I have tried
window.addEventListener("load", function () {
var you1 = document.getElementsByClassName("product-name");
var you5 = "";
for (var i = 0; i < you1.length; i++) {
var you2 = you1[i].textContent;
var you3 = you2.replace(/s/g, "");
var you4 = you3.replace("Xs", "");
you5 += you4;
}
var you6 = you5.includes("GetAllX");
if (you6 = "true") {
document.getElementsByClassName("wcf-bump-order-content")[0]
.setAttribute("style", "display:none !important");
}
console.log(you6);
console.log("finish");
});
Full code + HTML: https://jsfiddle.net/lindychen/14vLfcy5/
Results
As you can see from the JS fiddle, my code works and hides the relevant section. However, when I test this on my live website, it doesn’t work (ie. the upsell section still shows). I can’t figure out what’s wrong especially since console log shows you6
to be true
and finish
is also shown.
Thanks.
2
Answers
There are a couple of issues. First, you have a mistake in the conditional
if (you6 = "true")
should beif (you6 === true)
or justif(you6)
. The single equals vs. == or === and the string "true" vs the boolean valuetrue
.The second problem I found after fixing the conditional in the jsfiddle and it stopped working. The problem was using
<td>
tags without the requisite<table><tbody><tr>
around it was causing the query not to return a valid element list.After fixing that, the jsfiddle was working again. https://jsfiddle.net/evrmorr/k481pyjm/14/
Instead of
use
because
you6 = "true"
is an assignment, not a comparison and it will always be consideredtrue
unless you assign null or empty string to you6.For example:
however