1

I have the following HTML, and I need to add a class to both <li> and <a> where the text 'Charts' exists (so not the second line, but rather the 4th and 5th). I cannot change the output of the HTML.

<div class="wp-submenu-wrap">
    <div class="wp-submenu-head">Charts</div>
    <ul>
        <li class="wp-first-item">
            <a class="wp-first-item" tabindex="1" href="admin.php?page=charts">Charts</a>
        </li>
        <li>
            <a tabindex="1" href="admin.php?page=add-chart">Add Chart</a>
        </li>
    </ul>
</div>

I've tried doing this by locating the <a> tag that contains the text, but it is not working. Can somebody please point me in the correct direction? Thanks.

Code that I've tried -

$(document).ready(function(){

    if(pagenow === 'admin_page_edit-chart') {

        var page = $('.wp-submenu-wrap a:contains["Charts"]');

        page.addClass('current');
        page.parent('li').addClass('current');

    }

});
David Gard
  • 11,225
  • 36
  • 115
  • 227

4 Answers4

3

It should be :contains('text'). Other than that your code looks fine.

Anders Arpi
  • 8,277
  • 3
  • 33
  • 49
3

Your :contains() selector is invalid.

Change:

a:contains["Charts"]

To:

a:contains("Charts")

http://jsfiddle.net/C8KVW/

Curtis
  • 101,612
  • 66
  • 270
  • 352
2

The following code should do the job:

$(".wp-submenu-wrap a:contains('Charts')")
    .parent("li")
    .andSelf()
    .addClass("current");
VisioN
  • 143,310
  • 32
  • 282
  • 281
2

try this

    var page = $('.wp-submenu-wrap a:contains("Charts")');

    page.addClass('current');
    page.parent().addClass('current');
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40