Exclude element with specific attribute from jQuery selectors

I use this javascript to clear input / textarea when focusing.

$(document).ready(function() { $('input[type="text"],textarea').not('[readonly="readonly"]').addClass("idleField"); $('input[type="text"],textarea').focus(function() { $(this).removeClass("idleField").addClass("focusField"); if (this.value == this.defaultValue){ this.value = ''; } if(this.value != this.defaultValue){ this.select(); } }); $('input[type="text"],textarea').blur(function() { $(this).removeClass("focusField").addClass("idleField"); if ($.trim(this.value) == ''){ this.value = (this.defaultValue ? this.defaultValue : ''); } }); }); 

I am looking for a way to exclude those input fields that are set readonly via readonly="readonly" . I know I have to use .not (I think), but I cannot figure out how to do this.

+4
source share
1 answer

If you want all input elements to be writable (not only readable), you can use the following selector:

 $("input:not([readonly])") 

The parameter after : does not match the selector. [readonly] will match everyone where read-only is installed. Note that the following will not always work:

 [readonly="readonly"] 

How could you use both of these options:

 <input readonly type="text"> <input readonly="readonly" type="text"> 

You can also use ": input" psuedo-selector instead of "input, textarea". See the documentation here .

+7
source

All Articles