3

What I am wanting to do is add a class to a specific element with a specific url. But I can't seem to get it to work.

This is what I have.

var pageName = window.location.pathname
    pageName = pageName.replace("/", "");

$("div.mainleftmenu a.rtsLink.rtsRoot[href=pageName]").addclass("rtsSelected");

If it helps a.rtsLink.rtsRoot is several steps into the hierarchy. I'm new to jQuery.

Update

Thanks for the quick response, but I am still getting an error.

TypeError: Object [object Object] has no method 'addclass' [http://XXXXXXX/training-events:69]

Here is a sample of the markup I am trying to traverse. (And this is auto-generated so modifying the html is not an option.)

<div class="mainleftmenu">                       
    <div id="mainleftmenu_T76A8AA8B013">
        <div id="ctl00_mainleftmenu_T76A8AA8B013_ctl00_ctl00_siteMapControl_verticalsimple"
        class="RadTabStripVertical RadTabStrip_mainleftmenu RadTabStripLeft_mainleftmenu">
            <div class="rtsLevel rtsLevel1">
                <ul class="rtsUL">
                    <li class="rtsLI rtsFirst">
                        <a class="rtsLink rtsRoot" href="products">
                            <span class="rtsOut">
                                <span class="rtsIn">
                                    <span class="rtsTxt">Products</span>
                                </span>
                            </span>
                        </a>
                    </li>
                    <li class="rtsLI">
                        <a class="rtsLink" href="products/heavybid">
                            <span class="rtsOut">
                                 <span class="rtsIn">
                                    <span class="rtsTxt">HeavyBid</span>
                                </span>
                             </span>
                         </a>
                   </li>
                </ul>
             </div>
         </div>
    </div>
 </div>

2 Answers 2

3

You should pass the variable to the selector, currently you are passing a string:

$("div.mainleftmenu a.rtsLink.rtsRoot[href='"+pageName+"']").addClass("rtsSelected");

Also please note that you have written addclass which should be addClass.

Sign up to request clarification or add additional context in comments.

Comments

1

This should make it so that you don't use the string pageName, but you use the value of pageName:

$("div.mainleftmenu a.rtsLink.rtsRoot[href='"+pageName+"']")
                                               .addClass("rtsSelected");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.