Check if 2 variables contain exactly the same text and do action using jquery

jquery contains
jquery array contains
jquery each
jquery innerhtml contains
jquery check if child exists
jquery find
jquery array functions
if has element jquery

I have the code below and i want to check if the two variables "n" and "w" contain exactly the same text and do an action. However i can store in the variables the text string but when i try to set the conditional it doesn't work?

What am i doing wrong ?

 var d = new Date();
    var weekday = new Array(7);
    
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Wednesday";
    weekday[3] = "Thirsday";
    weekday[4] = "Πέμπτη";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    var n = weekday[d.getDay()];
    var w =  $('.today-tag').text();
   
  
    if(n === w) {
    $('.today-tag').text('TODAY');
    } else {
    $('.today-tag').text('');
    }
.today-tag {margin-left:10px;color:red;font-size:10px;font-weight:bold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="the-day">Sunday<div class="today-tag"></div></div>
<div class="the-day">Monday<div class="today-tag"></div></div>
<div class="the-day">Tuesday<div class="today-tag"></div></div>
<div class="the-day">Wednesday<div class="today-tag"></div></div>
<div class="the-day">Thirsday<div class="today-tag"></div></div>
<div class="the-day">Friday<div class="today-tag"></div></div>
<div class="the-day">Saturday<div class="today-tag"></div></div>

There are many div with class today-tag,so $('.today-tag').text() will not return the value you want.you can use each() to compare the value

    var d = new Date();
    var weekday = new Array(7);
    
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Wednesday";
    weekday[3] = "Thirsday";
    weekday[4] = "Πέμπτη";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    var n = weekday[d.getDay()];
    $(".today-tag").each(function(index,element){
     if($.trim($(element).parent().text())== n){
        $(element).text("Today");
      }else{
        $(element).empty();
      }
    });
.today-tag {margin-left:10px;color:red;font-size:10px;font-weight:bold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="the-day">Sunday<div class="today-tag"></div></div>
<div class="the-day">Monday<div class="today-tag"></div></div>
<div class="the-day">Tuesday<div class="today-tag"></div></div>
<div class="the-day">Wednesday<div class="today-tag"></div></div>
<div class="the-day">Thirsday<div class="today-tag"></div></div>
<div class="the-day">Friday<div class="today-tag"></div></div>
<div class="the-day">Saturday<div class="today-tag"></div></div>

.is(), A string containing a selector expression to match elements against. A function used as a test for every element in the set. 2. 3. 4. 5. <ul>. <li>list <strong>item 1</strong></li>. <li><span>list item 2</span></li> nothing will occur, because in those cases the target of the event would be "p" ).text( "It's a blue or red div. It is not 100% exact but it eliminates all strings that contain more than just the word I am looking for because I check for the string not containing individual spaces too. By the way you don't need these " ". jQuery knows you are looking for a string.

There is nothing in $('.today-tag').text(). You have to use the index of the current date in the array and parent() to get the text:

var d = new Date();
var weekday = new Array(7);

weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Wednesday";
weekday[3] = "Thirsday";
weekday[4] = "Πέμπτη";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
var idx = d.getDay();
var w =  $('.today-tag:eq('+idx+')').parent().text();

if(n === w) {
  $('.today-tag:eq('+idx+')').text('TODAY');
} else {
  $('.today-tag').text('');
}
.today-tag {margin-left:10px;color:red;font-size:10px;font-weight:bold;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="the-day">Sunday<div class="today-tag"></div></div>
<div class="the-day">Monday<div class="today-tag"></div></div>
<div class="the-day">Tuesday<div class="today-tag"></div></div>
<div class="the-day">Wednesday<div class="today-tag"></div></div>
<div class="the-day">Thirsday<div class="today-tag"></div></div>
<div class="the-day">Friday<div class="today-tag"></div></div>
<div class="the-day">Saturday<div class="today-tag"></div></div>

jQuery.contains(), Description: Check to see if a DOM element is a descendant of another DOM element. The $.contains() method returns true if the DOM element provided by the supported; if the second argument is a text or comment node, $.contains() will return false 2. $.contains( document.documentElement, document.body ); // true. Well I read your answer and it can be fine :-) ! +1. btw I think using indexOf is fine either, because here and here are two great answers from two profesional persons that both of him have used indexOf.

the .today-tag is an empty div element so we have to compare it with .the-day instead apart from that the $('.the-day') will return an array of elements thus we need to use the date() object there as well.. and lastly we just want today next to the current day so we use children() to return array of children of the selected day.. since you only have one child object we use 0th elemement.

i hope that explain's it well. (ps: i am no jquery expert been using vanilla js for long time thus there might be better alternative to children() method )

<script>
$(document).ready( function() {
    var d = new Date();
    var weekday = new Array(7);

    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Wednesday";
    weekday[3] = "Thirsday";
    weekday[4] = "Πέμπτη";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    var n = weekday[d.getDay()];
    var w =  $('.the-day')[d.getDay()];
    var today = $(w).children()[0]; //selects first child

    if(n === $(w).text()) {
    $(today).text('TODAY');

    } else {
    $(today).text('');
    }
});

jQuery.inArray(), indexOf() method in that it returns -1 when it doesn't find a match. to check for the presence of value within array , you need to check if it's not The following will return -1 (not found) because a number is being searched in an array of strings: <div>"Pete" is in the array, but not at or after index 2, so <span></span>​</div>. No necessary to check if a DIV contains a specific text, because whether has and not , which will return a collectoin of jquery object. using each method to reference corresponding jquery object then can use $ (this) as current jquery object. Following code will let you get your expected effect.

.filter(), A function used as a test for each element in the set. this is the current DOM The result of this call is a red background for items 2, 4, and 6, as they match This code will alter the first list item only, as it contains exactly one <strong> tag. Note: When a CSS selector string is passed to .filter() , text and comment nodes will  The More Functions category contains the categories Statistical, Engineering, Cube, Information, Compatibility, and Web. The Exact Function. The Exact function’s task is to go through the rows of two columns and find matching values in the Excel cells. Exact means exact. On its own, the Exact function is case sensitive.

jQuery Event Basics, jQuery offers convenience methods for most native browser events. 2. 3. 4. // Event setup using a convenience method will not have the same click behavior as its peers it can be useful to namespace your events so you don't unintentionally disconnect events that you didn't or couldn't know about. If we look at the table above shows first four rows, "aabc" repeated in 3 columns. So, out of 4, 3 gets correct matching.it is 3/4. for the second cycle, we can see "dbba" repeated so it 2/4.This is for 4 rows vs 4 rows, now the same matching method for taking one line down, two line down and three line down comparing with other string.

JavaScript Comparison and Logical Operators, Comparison operators can be used in conditional statements to compare values and take action depending on the result: if (age < 18) text = "Too young";. You will​  b) Every variable must have an associated type before you can use it. c) Every variable has a single type associated with it throughout its existence in the program, and the variable can only store values of that type. d) Variables are allowed to change type during their existence in the program as long as the value it currently stores is of the type it is currently declared to be.