Ask questions and get good answers on jQuery

I should use my function to select elements in the DOM via a jQuery Selector, how do I create my custom selector such as for example :enabled or :radio ?

asked Jan 23 '10 at 11:33

Federico%20Soldani's gravatar image

Federico Soldani
173128

edited Jan 25 '10 at 17:32


Here is an example on how to customize a selector in jquery:

// Add this code anywhere you want (after jQuery has been loaded). 
// Edit it to add your own expressions. 
jQuery.extend(jQuery.expr[':'], { 
    /////////////////////////////////////////////////////////// 
    // form elements that are submitable based on these criteria 
    //    element is not disabled 
    //    element has a selected or checked attribute 
    //    element is a textarea 
    //    element is an input of type text or hidden 
    // 
    //  @usage: $(':submitable') 
    //  @usage: $('#myForm :submitable') 
    submitable: function(a) { 
        return !a.disabled&&(a.selected||a.checked||(a.nodeName.toUpperCase()=='TEXTAREA')||(a.nodeName.toUpperCase()=='INPUT'&&(a.type=='text'||a.type=='hidden'||a.type=='password'))); 
    }, 
    /////////////////////////////////////////////////////////// 
    // elements that have a type attribute not equal to hidden 
    //    use if you want to select all input elements that are not hidden 
    //  @usage: $('input:nothidden') 
    nothidden: function(a) { 
        return a.type&&a.type!='hidden'; 
    } 
})

answered Jan 25 '10 at 23:07

Matteo%20Bicocchi's gravatar image

Matteo Bicocchi ♦♦
1776129

Thanks ... very helpful and explained well ...

(Jan 26 '10 at 19:25) Federico Soldani

That's pretty cool. I presume it works in both jQuery 1.3 and 1.4

answered Jan 27 '10 at 11:07

Maboa's gravatar image

Maboa
1

Your answer
toggle preview

jQuery main site - Matteo Bicocchi