Return to Snippet

Revision: 51326
at September 21, 2011 04:03 by betacar


Initial Code
jQuery.fn.hasAttr = function(selector) {
  if (this.attr(selector) !== 'undefined') {
    return true;
  } else return false;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * */

if ($('input[type="text"]').hasAttr('name^="date"')) { // Yay o nay?
  alert('Yay! =D');
} else {
  alert('Nay. =(');
}

Initial URL
http://forrst.com/posts/Is_this_attribute_has_this_value-vNA

Initial Description
Developing a form where I had several date and time fields, I came with a jQuery multiselector where I need to get, through the name attribute, if it was a date or time field. So far, jQuery does not have a boolean method to check if an attribute of an element has a certain value.

This custom method does that. It simple retuns true if the attribute element has a certain value. False if it doesn't.

Initial Title
Check if a element attribute has a certain value

Initial Tags
javascript, jquery

Initial Language
JavaScript