• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JQuery Disabled Selector Example

May 23, 2014 //  by Krishna Srinivasan//  Leave a Comment

JQuery disabled selector selects all the disabled elements in the form. To select all the elements in the form, by default we use “:disabled”. But, however, we can filter the elements by prefixing the component name or type like “input:disabled”,”select:disabled”,etc. The disabled attribute selector is a boolean attribute, where “true” value indicates element is disabled and “false” value indicates element is not disabled.

Disabled attribute can be used within <input> element. We can write as disabled=”disabled” in the input element. The disabled elements will not be submitted in a form. For instance, consider a line with disabled attribute <input type=”text” name=”fname” disabled= “disabled”>. When we write disabled, it specifies that input field is disabled and can’t do any operation on that element. The disabled selector supported by some of the HTML elements such as button, select, div, option, input and textarea elements.

JQuery Disabled Selector Syntax

$(“:disabled”)

JQuery Disabled Selector Example

<!doctype html>
<head>
<title>JQuery Disabled Selector</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Disabled Selector Example</h2>
<style>
.test
{
color:green;
}
div
{
background:lightblue;
}
</style>
<script>
   $(document).ready(function(){
        $("#Disable").click(function(){
  	  $(".myexample *").prop('disabled',true);
   	});
	$("input:disabled").css("border-color", "red");
   });
</script>
<body>
<form>
Name:<input type="text" name="uname" class="test" disabled="disabled" value="This is disabled textbox"><br>
Password:<input type="password" name="pwd"><br><br>
<input type="submit" value="Submit" class="test" disabled="disabled">
</form><br><br>

<div class="myexample">
    Fname:<input type="text" placeholder="First Name"/>
    Lname:<input type="text" placeholder="Last Name"/>
</div>
<input type="button" id="Disable" value="Disable the Elements"/>
</body>
</html>
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“#Disable”).click(function()) line defines click method which occurs when element is clicked which uses id attribute of input element.
  • $(“.myexample *”).prop(‘disabled’,true); line sets the disabled property to true. The true value defines that element will be disable after clicking the button element. The myexample * is the class of div element which makes all the div elements disabled by using * symbol in the web page.
  • $(“input:disabled”).css(“border-color”, “red”); line defines disabled selector which selects all the elements of type disabled and uses input HTML element that support the disabled attribute.
  • When we run the above script, we will get two input type elements. One with disabled and second one with enabled and the button also will get display as disabled. The disabled elements will get display with green color text and red color border as specified in the input element and CSS class test. A button will get appear on the browser, when we click on that button all the elements will get disabled which are defined under the div element.

JQuery Disabled Selector Demo

When you run the above example, you would get the following output:

  • Run JQuery Disabled Selector Demo

JQuery Disabled Selector Example

It is also possible to disable the select and textarea elements by using div element. The following example shows how to disable the select and textarea elements.

<!doctype html>
<head>
<title>JQuery Disabled Selector</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Disabled Selector Example</h2>
<style>
div
{
background:lightgray;
}
</style>
<script>
    $(document).ready(function(){
	$("#mydiv").find("select,textarea").prop('disabled',true);
   });
</script>
<body>
<div id="mydiv">
    <textarea>This is disabled</textarea><br>
    <select>
       <option>Item1</option>
       <option>Item2</option>
       <option>Item3</option>
    </select>
</div>
</body>
</html>

The above scripts specifies the disable of select and textarea elements. The find() method returns descendant elements of selected elements in the set of matched elements.The prop method returns properties and values of the selected elements. When we run the script, the find method selects elements by using id of the div element and prop method defines disable property with true value.If it is true, then all elements will be disabled on the web page and if it is set to false, then all the elements will be enabled.

When you run the above example, you would get the following output:

  • Run JQuery Disabled Selector using Div Demo

DisabledSelector2Picture

Category: jQueryTag: JQuery Selectors

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Accessing Data with Spring Data GemFire
Next Post: Caching Data using Spring Data GemFire »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact