I have this simple jQuery code to test out.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("text").attr("disabled","");
});
});
</script>
</head>
<body>
<input type="text">
<br />
<button>Set the textfield disabled</button>
</body>
</html>
Basically the HTML page comes with a simple button and textfield. All I want to have the input field disabled as I click the button. But it doesn't work???
(PS: this code is sourced out from w3schools.com website, just to simply test out how powerful jQuery is)
To make a table in HTML, use the <table> tag. Within this table tag, you'll place the <tr>, <th>, and <td> tags. The <tr> tag defines a table row. The <th> tag defines the table header.
The disable/enable an input element in jQuery can be done by using prop() method. The prop() method is used to set or return properties and values for the selected elements. Example 1: This example uses prop() method to disable the input text field.
You can use $(":disabled") to select all disabled items in the current context. To determine whether a single item is disabled you can use $("#textbox1").is(":disabled") . Save this answer.
We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.
From jQuery 1.7, you could use .prop
:
$(document).ready(function(){
$("button").click(function(){
$(":text").prop("disabled", true);
});
});
Before 1.7, you could do:
$(document).ready(function(){
$("button").click(function(){
$(":text").attr("disabled", true);
});
});
PS: Use $(":text")
or $('input[type="text"]')
to select all elements of type text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With