Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude read only fields using :input in jquery?

thus far i've only been using some basic jquery selectors and functions. but i'm looking at this clear form function and i can't figure out how to add it so i can remove hidden inputs and readonly input from getting cleared.

can anybody help? thanks.

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};
like image 692
melaos Avatar asked Apr 11 '10 13:04

melaos


People also ask

How do I remove the readonly property in jQuery?

How do I remove the readonly property in jQuery? jQuery code by using attr method : $('#emp'). attr('readonly', false);

How can input field readonly in jQuery?

To make a textarea and input type read only, use the attr() method .

How do I remove a readonly attribute?

Click Security tab and then click Edit button. Step2. Choose the user name that you use to set readonly attribute previously and select the Permissions for system. Then, click OK button to confirm to remove the readonly attribute.

How check textbox is readonly or not in jQuery?

Learning jQuery One of way is to make textbox readonly is to set "readonly" attribute to true. So use the same attribute "readonly" to find out whether textbox is readonly or not using jQuery. $(document). ready(function(){ $("#txtReadonly").


2 Answers

If the elements have a readonly attribute in their declaration, you can use jQuery’s :not() selector:

$(':input:not([readonly])', form)

Otherwise filter the read-only elements with something like this:

$(':input', form).each(function() {
    if (this.readOnly) return;
    // …
});
like image 117
Gumbo Avatar answered Oct 25 '22 03:10

Gumbo


<script type="text/javascript" language="JavaScript">
    jQuery(document).ready(function(){
        try {
            var t = jQuery("input[type=text]").not("[readonly]").focus(function(){
                jQuery(this).blur();
            });
            alert(t.length);
        }
        catch(error){
            alert("error :" + error);
        }
    });
</script>
like image 36
Omar Abou Ammar Avatar answered Oct 25 '22 01:10

Omar Abou Ammar