Home JQuery How to disable all html form elements within a DIV tag using JQuery?
 
 
 

How to disable all html form elements within a DIV tag using JQuery?

PDF Print E-mail

Question: How to disable all html form elements within a DIV tag using JQuery?

 

NOTE: Please not that the prop JQuery function should be used instead of the attr function in JQuery 1.6 and higher

 

Answer

To disable all input elements within div use the following code: 
$('#message :input').attr('disabled', true);

To disable all select elements within div use the following code: 
$('#message :select').attr('disabled', true);

To disable all submit buttons within div use the following code: 
$("#message :submit").attr("disabled", "true");

Explanation:

  • The DIV tags ID is message    
  • attr- Access a property on the first matched element.
  • $("selector")
  • If disabled is set to false the element won't be disabled
+/-
  • Ken Collins - Your Doing It Wrong
    The jQuery docs say to use prop() for things like disabled, checked, etc. Also
    the more concise way is to use their selectors engine. So to disable all form
    elements in a div or form parent.

    $myForm.find(':input:not(:disabled)').prop('disabl ed',true)
  • TechPortal - re: Your Doing It Wrong
    Ken Collins wrote:
    The jQuery docs say to use prop() for things like disabled, checked, etc. Also
    the more concise way is to use their selectors engine. So to disable all form
    elements in a div or form parent.

    $myForm.find(':input:not(:disabled)').prop('disabl ed',true)


    I a matter of fact it was not wrong prior to JQuery 1.6 when this article got
    written, but thank you for pointing it out. If JQuery 1.6 or higher is used then
    the prop method is the one to use.
  • kunal gupta - Nice one
    Thanks i got my solution by this post.
  • amer - appreciate
    awesome code
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!