2

I'm trying to highlight all fields that contain a certain string exactly, in the example below I will also match someuser2, someuser3 etc.

$("div.forumbody .username:contains('someuser')").css("background", "#6a5acd");

How can I search for the exact string and make the background of the field #6a5acd? Thanks in advance.

3 Answers 3

3

Try this

Create custom selector

$.expr[':'].textEquals = function(a, i, m) {
return $(a).text().match("^" + m[3] + "$");
};

and use it

$("div.forumbody .username:textEquals('someuser')").css("background", "#6a5acd");
Sign up to request clarification or add additional context in comments.

Comments

1

That looks right to me... Make sure your selectors are working right without the contain...

$("div.forumbody .username").css("background", "#6a5acd");

EDIT

For text fields:

$("div.forumbody .username[value*=someuser]").css("background", "#6a5acd");

Or if they are not prepopulated (building off Govind's answer) :

$.expr[':'].valHas = function(a, i, m) {
  return $(a).val().match("^" + m[3] + "$");
};

$("div.forumbody .username:valHas('someuser')")

DOUBLE EDIT

This tested working and simpler:

jQuery("div.forumbody .username:text[value*='someuser']").css("background", "#6a5acd");

Comments

0

Try add a extend pseudo function:

$.expr[':'].textEquals = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().match("^" + arg + "$");
    };
});

Then you can do:

$("div.forumbody .username:textEquals('someuser')").css("background", "#6a5acd");

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.