开发者

how to highlight the row regardless of case sensitive using jquery

开发者 https://www.devze.com 2023-04-13 06:40 出处:网络
$(document).ready(function () { $(\"#btnhighlight\").click(function () { var htext = $(\"#txthighlighttext\").val();
$(document).ready(function () {
                $("#btnhighlight").click(function () {
                    var htext = $("#txthighlighttext").val();
                    if (htext == '') {
                        alert("Pleae enter the search item.");
                        return false;
                    }
  开发者_如何学C                  $("#lstCodelist option").each(function () {
                        $(this).removeClass('searchItem');
                        if ($(this).text().search(htext) != -1) {
                            $(this).addClass('searchItem');
                        }
                    });
                });
            });

Lets take I have a row something like this

I love  to work with Jquery.

If I enter my search text as jquery its not highlighting Jquery. But my query should work in both they way regardless of CAPS or SMALL letters.

how to change my code to work like that.

thanks for your all help.


use .toUpperCase() ............. // or lowerCase

  if ($(this).text().toUpperCase().indexOf(htext.toUpperCase()) != -1) {


This one should work, I believe:

$(document).ready(function () {
    $("#btnhighlight").click(function() {
        var htext = $("#txthighlighttext").val().toLowerCase();

        if (htext === '') {
            alert("Please enter the search item.");
            return false;
        }

        $("#lstCodelist option").each(function() {
            var $this = $(this);

            if ($this.text().toLowerCase().indexOf(htext) !== -1) {
                $this.addClass('searchItem');
            } else {
                $this.removeClass('searchItem');
            }
        });
    });
});

Sidenote: indexOf is proven to be faster than search.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号