1

Possible Duplicate:
CSS selector case insensitive for attributes

The here below code checks if an attribute of a given class, starts with the value that the user has entered in a given input

var inputVariable = $('#myInput').val();

$('.class[attribute ^= '+inputValue+']')......

My problem is that the attribute "verification' is case sensitive. I am looking for a solution to make it not case sensitive. Hope someone can help. Thank you in advance for your replies. Cheers. Marc

Community
  • 1
  • 1
Marc
  • 9,217
  • 21
  • 67
  • 90

2 Answers2

2

Try this:

var re = new RegExp("^"+inputValue, "i");
    $('.class').filter(function() {
        return (re).test( $(this).attr('attribute'));
    });
Matteo-SoftNet
  • 531
  • 3
  • 10
  • and what if the class on the DOM element is in all uppercase ! I think your miss reading the question – Manse Mar 06 '12 at 16:22
  • Hello DotMat. Thanks for helping out. The problem with your solution is that it only works in one direction. The thing is, the user can either enter a string that starts with an uppercase or lowercase in the input. So I need something working in the two directions... But anyway thanks for sharing this toLowerCase thing. I did not knew it. – Marc Mar 06 '12 at 16:23
  • Making the value lower case doesn't prevent the selector from being case sensitive... If the value of the attribute is "teSt" and the value of `inputValue` is "Test", the value of `inputValue.toLowerCase()` will be "test" and they still won't match. – Anthony Grist Mar 06 '12 at 16:23
  • Hello Anthony. Correct. Do you know a way to work around this? – Marc Mar 06 '12 at 16:27
  • Sorry for misunderstanding, I've just completed my answer. – Matteo-SoftNet Mar 06 '12 at 16:29
  • @DotMat **read the question again** .... what if the class on the DOM element is "SOMEthing" .... what the OP needs is the search to be case-insensitive not convert everything to lowercase before searching – Manse Mar 06 '12 at 16:30
1

Use RegExp to compare the value of the attribute. See code: http://jsfiddle.net/djBsy/2/

user1242756
  • 231
  • 1
  • 6
  • Hello there. Your example is very interesting. I learned things. Nevertheless it is not working. if you type for instance selectorCaseInsensitive('.myClass', 'id', 'hsduqshudihqduisu'); the result is the same.... – Marc Mar 07 '12 at 12:23
  • Thanks for the hint. The if condition was incorrect. I updated it and it works now :) – user1242756 Mar 07 '12 at 12:43
  • Your example made me understand how all this works (regexp...). So by adapting your example by modifiying it a bit I manage to get what I wanted. So thanks again :) – Marc Mar 07 '12 at 12:59