Joshua Smith Joshua Smith - 1 year ago 207
jQuery Question

JQuery select by inner text

I am doing a project with fullcalendar and I need to be able to select an element based on the day number. Here is a sample of the element I need to select:

<div class=​"fc-day-number">​1​</div>​

Here is the javascript I have written:

$("div.fc-day-number:contains(" + day + ")").parent().parent().css({ 'background-color': 'Green' });

This will grab all the divs whose inner text contains the day variable, problem is if day==1 then it also selects 11,12,13,21,31,etc. How do I write a JQuery selector that will grab the divs with class "fc-day-number" and whose inner text is exactly equal to day?

Answer Source

Write your own filter function:

$("div.fc-day-number").filter(function (){
    return $(this).text() == day;
}).parent().parent().css({ 'background-color': 'Green' });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download