Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one match an element in jQuery without needing to match case?
    primarykey
    data
    text
    <p>I have a page with some navigation and I'm trying to highlight the link if that's the page we're on. It works almost quite well just like this:</p> <pre><code>function highlightPageInNav() { // Get file name. var filename = window.location.href.substr(window.location.href.lastIndexOf("/") + 1); // Build selector. var attrSelector = "a[href='" + filename + "']"; // Set class of li to "current". $("#nav ul li").has(attrSelector).addClass("current"); } </code></pre> <p>The problem is that the anchors in my navigation say, for example, "CreateUser.aspx", but the script won't match that if the current URL contains "createuser.aspx". </p> <p>I've tried several other ideas, including using the following to replace part of the script above. I convert the filename toLowerCase() and then try to match it to the href .toLowerCase() and those alerts show me what I expect, but the <code>$(this).addClass("current");</code> doesn't actually do anything -- most likely, I guess, because <code>$(this)</code> no longer applies to the element I'm trying to modify.</p> <pre><code>$("#nav ul li a").each(function () { alert($(this).attr("href")); hrefLowerCase = $(this).attr("href").toLowerCase(); alert(hrefLowerCase); if (filename == hrefLowerCase) { alert("found one!"); $(this).addClass("current"); // This is where it stops doing what I desire. } }); </code></pre> <p>So, if someone can show me how to very simply make my script match both "CreateUser.aspx" and "createuser.aspx", that would be much appreciated! Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload