A jQuery hasAttr() Equivalent

Submitted by:J Kamy

Date added:14 February, 2017


jQuery doesn't really have an .hasAttr() function. You might assume that it does, but alas, it does not.

Tags: jquery function

Code Snippet:

// Get the attribute, check the value

var attr = $(this).attr('name');

// For some browsers, `attr` is undefined; for others, `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
// Element has this attribute

//Native JavaScript has a way
// If you only have a jQuery reference...


// Filter the selection