views:

112

answers:

2

I'm working on a WordPress site that uses a dynamically generated menu system which I can't modify becauase of future updates, but what I'd like to do is be able to change the style of the text within one <span> in that menu with jQuery on document ready. The span is nested as an <li> item, and I don't if that makes a difference. (And I don't see WordPress making a difference, either.) There are other <span>'s and I don't want them to change, so I need to match the specific text within the <span> I want to change.

So how could I change this <span>We are here to help.</span>

to this

<span style="color:#123456;">We are here to help.</span>

on $(document).ready ?

The full html is

<ul id="nav"><li class="li_item"><a class="navlink" href="http://www.mysite.org/contact-us/"&gt;&lt;strong&gt;Contact Us</strong><span>We are here to help.</span></a></li></ul>

Update: This works; need to wrap the function in an alias for no conflicts:

jQuery(function($) {
$(document).ready(function() {
$("#nav li span:contains('We are here to help.')")
.css("color", "#f8481c"); 
});
});
+4  A: 

Try this (assumes nav is the id of the ul for your menu system):

$(document).ready(function() {
    $("#nav li span:contains('Text text text')")
      .css("color", "#123456"); 
});

For your example this jsFiddle works:

Graphain
Hmmm; looks good but doesn't want to work for me. I added the full html above; does it matter if the #nav is a ul? Thanks...
songdogtech
There was a syntax error (missing semi-colon at end) that I've amended.
Graphain
Added jsFiddle example
Graphain
Flustrating. jQuery is loading fine (from Google, actually). But no luck and an error: "Result of expression '$' [undefined] is not a function" Could this be getting stomped on by something else? But jsFiddle is very cool, will use that in the future.
songdogtech
Try replacing `$` with `jQuery`. Alternatively you have Prototype or something similar loading first and killing jQuery.
Graphain
Does Firebug show jQuery as loaded? Does your page validate (or at least "kinda validate") ?
Graphain
Firebug shows jQuery loading, and I disabled some other jQuery scripts. No Prototype. Have a bunch of missing alt tags, etc, but no major validation errors. There is an existing problem with a jQuery slider not working, so they might be related. Site is demon-slayer.org Have to crash now, but thanks for your help and I will be back at this tomorrow.
songdogtech
Wordpress runs jQuery in `noconflict` mode which means you can't use `$` you have to use `jQuery`. Replace all instances of `$` with `jQuery` and you should be fine.
Graphain
or wrap up with `jQuery(function($) { ... });` instead of the document ready... `jQuery(function)` is an alias for `jQuery(document).ready(function)` and the called function receives jQuery as its first parameter, so you can safely use the `$` inside
gnarf
Works! Wrapping in the alias might also work for the other jQuery problems. One more detail: in that type of construct .css("color", "#123456") how wold you add another rule, i.e. font size? Thanks!
songdogtech
@songdogtech - two ways, one by chaining (e.g. `.css('prop', value').css('prop2', value2')`), the second (probably better way) by using a map: `css({"color":"#123456", "font-size":"12px"})`. See http://api.jquery.com/css/
Graphain
Thanks! Learning slowly...
songdogtech
A: 

This should work if the :contains isn't working for you.

$(function() {
  $('#nav li span').each(function() {
    if ($(this).text() === 'We are here to help.')
      $(this).css('color', '#123456');
  });
});

Edit: too many parentheses.

Mitch R.
Actaully, this throws an error: Result of expression '$' [undefined] is not a function?
songdogtech