phpN00b
phpN00b

Reputation: 807

jquery change == to contains?

I'm trying to alter the code below so that if "this" contains the class "search_init", it will only replace that class, as the markup itself has multiple classes applied to it. I tried ~= and ^=, but that gives me errors about missing ().

$("tfoot input").focus( function () {
            if ( this.className == "search_init" )  {
                this.className = "";
                this.value = "";
            }
        } );

Upvotes: 1

Views: 194

Answers (3)

user212571
user212571

Reputation: 31

Should be a one liner:

$('tfoo input.search_init').focus(function(e) {$(this).removeClass('search_init').val('');});

As an aside, you could change the styling with a .search_init:focus css meta-class.

Upvotes: 1

Gordon Gustafson
Gordon Gustafson

Reputation: 41209

$("tfoot input.search_init").focus( function () {
                $(this).removeClass("search_init");
                this.value = "";
            }
        } );

~= "Selects elements that have the specified attribute with a value containing a given word, delimited by spaces." This would work, but it makes much more sense to use built in class selectors than mess with the class attribute itself. Likewise with ^=, which "Selects elements that have the specified attribute with a value beginning exactly with a given string."

Upvotes: 1

PetersenDidIt
PetersenDidIt

Reputation: 25620

Try this out:

$("tfoot input").focus( function () {
    var $this = $(this);
    if ( $this.hasClass("search_init") )  {
        $this.removeClass("search_int").val('');
    }
});

Upvotes: 5

Related Questions