Posted By

betacar on 09/21/11


Tagged

javascript jquery attribute selector


Versions (?)

Check if a element attribute has a certain value


 / Published in: JavaScript
 

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

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.

  1. jQuery.fn.hasAttr = function(selector) {
  2. if (this.attr(selector) !== 'undefined') {
  3. return true;
  4. } else return false;
  5. }
  6.  
  7. /* * * * * * * * * * * * * * * * * * * * * * * * * * * */
  8.  
  9. if ($('input[type="text"]').hasAttr('name^="date"')) { // Yay o nay?
  10. alert('Yay! =D');
  11. } else {
  12. alert('Nay. =(');
  13. }

Report this snippet  

You need to login to post a comment.