In this post you will learn how to disable and enable different HTML elements (input, textarea, links, etc.) using jQuery. First, let's categorize the diffent types of elements that can be disabled using jQuery. We can categorize them into 3 general categories. Method of disabling items in each category is different.
If I missed some other element that does not fall under any of those 3 categories please leave a comment. The idea behind any method is very simple though. Usually all you have to do is to change element's attribute. See example below to get an idea.
Here are the three categories that we mentioned:
- Form elements - input fields, textarea, buttons, select boxes, radio buttons, etc.
- Anchor links - make text links non clickable.
- Bound jQuery events - for example bound
click
event on a<div>
.
1. Disable form elements
Consider you have a form and you need to disable some element on it. All you have to do to disable it is to add disabled
property to that element (input
, textarea
, select
, button
). Let's see an example:
<form action="url" method="post">
<input type="text" class="input-field" value=".input-field">
<input type="button" class="button-field" value=".input-field">
<input type="radio" class="radio-button" value=".input-radio">
<select class="select-box">
<option value="1">One</option>
<select class="select-box">
</form>
jQuery code to disable form elements and enable them back:
// jQuery code to disable
$('.input-field').prop('disabled', true);
$('.button-field').prop('disabled', true);
$('.radio-button').prop('disabled', true);
$('.select-box').prop('disabled', true);
// To enable an element you need to either
// remove the disabled attribute or set it to "false"
// For jQuery versions earlier than 1.6, replace .prop() with .attr()
$('.input-field').prop('disabled', false);
$('.button-field').removeAttr('disabled');
$('.radio-button').prop('disabled', null);
$('.select-box').prop('disabled', false);
Caveats & notes
Setting form element's disabled
attirbute causes browsers not to sent that element to the server on form submit. In other words, your server script will not receive that form element's value. The workaround to that problem is to sent readonly
attribute instead of disabled
. This will make your fields non editable, but browser will still send the data to the server.
2. Disable anchor link (<a href="" ...>)
Now, let's see how to disable a link on your page, so that when user clicks on it browser does not follow it. There 2 methods to do that:
- Catch
click
event and prevent default bahaviour; - Replace link's
href
property to "#".
I personaly prefer the first method, but you may have different needs. Let's see both methods in action.
<!-- Consider we have this HTML -->
<a href="aboutus.html" class="internal">some internal link<a>
<a href="http://www.google.com" class="external">external link<a>
// Bind "onclick" event
$('.internal').on("click", function(e){
e.preventDefault();
return false;
});
// Replace link's "href" attribute
$('.external').prop('href', '#');
Caveats & notes
In the onclick
example above we added e.preventDefault()
method which would stop event propagation. If you have other events relying on it on parent elements, please remove that method call. Also, when setting link's new href attribute, you can save the initial value with .data()
method in order to set it back later.
// Removing e.preventDefault();
$('.internal').on("click", function(e){
return false;
});
// Recording link's "href" attribute for later use
$('.external').data('original-href', $('.external').attr('href'));
$('.external').prop('href', '#');
// Setting it back
$('.external').prop('href', $('.external').data('original-href'));
3. Unbinding bound jQuery events
Last but not least is unbinding previously bound events. This is probably the easiest of the batch. All you have to do is to use jQuery's .unbind()
method. Let's see an example:
<div class="some-elem">
Click me
</div>
// Bind "click" event
$('.some-elem').on('click', function(){
alert("Div is clicked!");
});
// Unbind "click" event
$('.some-elem').unbind('click');
Caveats & notes
Unbinding click
event will unbind all click
events that were bound to that element. So, if you want to unbind only your click
event, without affecting others you have 2 options:
- Namespace your events;
// Namespacing events $('.some-elem').on('click.my_event', function(){ alert("Div is clicked!"); }); // Unbind namespaced event $('.some-elem').unbind('click.my_event');
- Use function reference when binding and unbinding.
// User function reference var my_func = function(){ alert("Div is clicked!"); }; // Bind using function reference $('.some-elem').on('click', my_func); // Unbind namespaced event $('.some-elem').unbind('click', my_func);