vektor vektor - 10 months ago 32
Javascript Question

Find elements where name is different than id using jQuery

How do I write jQuery expression that will find inputs where the

attribute value is different from the
attribute value?

This should be found

<input name="foo" id="bar" />

This should not be found

<input name="foo" id="foo" />

Trivial stuff like
fails, because the
expression expects a constant on the right hand side.


You can do this using filter() function. And use trim to make sure.

Edit Simple version

    return !=;

If there are any issues because of trailing or leading spaces, then the following can be used.

    thisID = $.trim(;
    thisName =  $.trim(;
    return thisID != thisName;