I am trying to disable some input fields using jQuery 1.4.2. This is a part of the HTML code:
<input type="text" disabled="disabled" name="nume" value="Text" />
This is the Javascript:
$('.salveaza').live('click', function(e){
$.get(ajaxURL, $('form', itemCurent).serialize()+'&id='+itemCurent.data('id')+'&optiune=editeaza-categorie');
// This is the code for disabeling inputs
$('input', itemCurent).attr('disabled', 'disabled');
itemCurent.removeClass('curent');
$('.optiuniEditeaza', itemCurent).remove();
e.preventDefault();
});
The above code doesn't work. Note that the inputs are added with jQuery, that's why I am using live, I tried all sorts of stuff.
I also tried firebug console to disable all input fields on the page and it's not working:
$('input').attr('disabled', 'disabled');
Any help will be apreciated, Thanks
I am trying to disable some input fields using jQuery 1.4.2. This is a part of the HTML code:
<input type="text" disabled="disabled" name="nume" value="Text" />
This is the Javascript:
$('.salveaza').live('click', function(e){
$.get(ajaxURL, $('form', itemCurent).serialize()+'&id='+itemCurent.data('id')+'&optiune=editeaza-categorie');
// This is the code for disabeling inputs
$('input', itemCurent).attr('disabled', 'disabled');
itemCurent.removeClass('curent');
$('.optiuniEditeaza', itemCurent).remove();
e.preventDefault();
});
The above code doesn't work. Note that the inputs are added with jQuery, that's why I am using live, I tried all sorts of stuff.
I also tried firebug console to disable all input fields on the page and it's not working:
$('input').attr('disabled', 'disabled');
Any help will be apreciated, Thanks
Share Improve this question edited Mar 4, 2012 at 2:13 rob74 5,26833 silver badges37 bronze badges asked Jun 1, 2010 at 17:34 AndreiAndrei 4,6174 gold badges28 silver badges25 bronze badges 2-
That would seem to be the correct procedure for disabling an
input
element, as long as theinput
is a child ofitemCurent
. Are you certain that the code after your$.get()
is running? Does the request succeed? – user113716 Commented Jun 1, 2010 at 18:02 - Yes the input is a child of itemCurent, I also tried disabling all the inputs on the page and it just enables all of them. – Andrei Commented Jun 2, 2010 at 8:34
4 Answers
Reset to default 3This should do the trick:
Disable: $('#the-field-id').attr('disabled', true);
Enable: $('#the-field-id').removeAttr('disabled');
Use true
instead of disabled
:
$("input").attr("disabled", true);
Working example: http://jsfiddle/bEC8L/ (input is set to disable after 5 seconds)
jQuery sets properties instead of attributes if the given attribute is actually a property name, and "disabled"
isn't a valid value for the disabled
property.
Your piece of code seems ok, so the problem must be ing from another part of code. Put a break on the line where you disable and then run step by step.
Try this code. Onclick of button the textbox will be disabled.
<script type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#fname").attr("disabled",false);
$("#idDisable").click(function(){
$("#fname").attr("disabled",true);
});
});
</script>
<input type="button" id="idDisable" value="click to disable" name="Click"/>
<input type="text" id="fname" />