I am trying to add a "title" attribute to HTML td elements based on their text.
I have written the following:
$("tr.sel-rtac-first td:first-child").each(function(t,x){
var hours = "";
var text = $(x).text();
switch (text) {
case (text.includes("מרכזית-מדף פתוח") == true):
hours = "יהדות 401: א - ה 08:00 - 19:00"
break;
default: hours = "text"
}
$(x).attr("title",hours);
});
I can see in my tests that the text variable is receiving the correct text:
"
Cen.lib-Stacks - מרכזית-מדף פתוח -#Four weeks - ארבעה שבועות#
"
The text is in Hebrew, but you can clearly see that the text I am looking for is indeed part of the variable text. Yet it reverts to the default, not recognizing that the text includes that segment.
I also tried without the == ture in the code:
case (text.includes("מרכזית-מדף פתוח")):
But this doesn't work either.
A previous solution I tested was to use the .title() function, but that didn't work either (I suspect because the element doesn't have a title attribute to begin with, I am adding one).
How can I make sure that the includes function recognizes that the substring is indeed in the text?
if {} else {}instead ofswitch. You're switching based on the text string, and the first case, while it evaluates to true, then says basically does the stringtextmatch the booleantrue, which it doesn't so it fails. You want something likeif(text.includes("מרכזית-מדף פתוח")) { hours = "19:00 - 08:00 ... "; } else { hours = "text"; }