Extend jQuery selector types


/ Published in: JavaScript
Save to your folder(s)

Extend jQuery :selector types

Ex: $("input:text")
$("input:email")


Copy this code and paste it in your HTML
  1. $.extend($.expr[':'], {
  2. email: function(em) {
  3. return $(em).attr("type") === "email";
  4. }
  5. });
  6.  
  7. // ':textall' jQuery pseudo-selector for all text input types
  8. // source: http://markdalgleish.com/2011/05/jquery-selector-for-html5-input-types/
  9.  
  10. (function($) {
  11. var types = 'text search number email datetime datetime-local date '
  12. + 'month week time tel url color range'.split(' '),
  13. len = types.length;
  14. $.expr[':']['textall'] = function(elem) {
  15. var type = elem.getAttribute('type');
  16. for (var i = 0; i < len; i++) {
  17. if (type === types[i]) {
  18. return true;
  19. }
  20. }
  21. return false;
  22. };
  23. })(jQuery);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.