Reputation: 1
I am having some difficulty with the jQuery :contains selector. I am making a calendar and I have some code that will highlight the current date on the calendar, which is stored in a variable called tdate. I'm printing the dates in my calendar in <td>s with a class of "day", and I have a set background-color for each month stored in a varibale called monthBG. Here's where the issue is in my code:
$('.day:contains("' + tdate + '")').css({'background-color': monthBG, 'color': 'white'});
The issue with this is that with single-digit dates, it will highlight the days that include that number in their date, but aren't the current date (for example, 6, 16 and 26 all have the number 6 in them, so if it was the 6th of any given month, all the dates listed would be highlighted with my current code).
So, how would I select something that contains the text value EXACTLY equal to the value of the tdate variable?
Upvotes: 0
Views: 27
Reputation: 11328
For exactly equal text/value search you could use something like this:
$('.day').filter(function() {
return $(this).text()===tdate;
}).css({'background-color': monthBG, 'color': 'white'});
Demo:
tdate='1111';
monthBG='red';
$('.day').filter(function() {
return $(this).text().trim()===tdate;
}).css({'background-color': monthBG, 'color': 'white'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='day'>
1111
</div>
<div class='day'>
2222
</div>
Upvotes: 1