Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use jQuery to ignore case when selecting?

I'm currently attempting to disable a link using the following jQuery selector:

$("a[href$=/sites/abcd/sectors]").removeAttr("href");

The problem is that sometimes the href might not always be lower case on the page. When this happens the selector no longer matches.

Does anyone know how to get around this? Can I change the behaviour this once to ignore case?

like image 641
Alex Angas Avatar asked Mar 06 '09 17:03

Alex Angas


People also ask

Is jQuery ID selector case sensitive?

jQuery attribute value selectors are generally case-sensitive.

Can you use CSS selectors in jQuery for selecting elements?

Projects In JavaScript & JQueryjQuery uses CSS selector to select elements using CSS. Let us see an example to return a style property on the first matched element. The css( name ) method returns a style property on the first matched element.

Which is the jQuery selector function used for selecting the elements?

The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.


2 Answers

I ran into this myself. I switched the logic a bit to allow me to compare it without case. It requires a little more work, but at least it works.

$('a').each(function(i,n) {
    var href = $(n).attr("href");
    href = href.toLowerCase();
    if (href.endsWith('/sites/abcd/sectors'))
        $(n).removeAttr('href');
});

You would have to figure out your own endsWith logic.

like image 84
EndangeredMassa Avatar answered Sep 21 '22 19:09

EndangeredMassa


jQuery was built to be extended. You can correct it or add your own type of case-insensitive selector.

Rick Strahl: Using jQuery to search Content and creating custom Selector Filters

like image 39
Josh Stodola Avatar answered Sep 19 '22 19:09

Josh Stodola