3

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

2
  • That would seem to be the correct procedure for disabling an input element, as long as the input is a child of itemCurent. Are you certain that the code after your $.get() is running? Does the request succeed? 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. Commented Jun 2, 2010 at 8:34

4 Answers 4

3

This should do the trick:

Disable: $('#the-field-id').attr('disabled', true);

Enable: $('#the-field-id').removeAttr('disabled');

Sign up to request clarification or add additional context in comments.

1 Comment

At your input field, You must set an ID or class to call that action! [ $('#something') -> call ID "something" ] [ $('.something') -> call class "something" ] [ # -> for ID ] [ . -> for Class ]
2

Use true instead of disabled:

$("input").attr("disabled", true);

Working example: http://jsfiddle.net/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.

4 Comments

I tried using true and it's not working, I used the code you gived me and it enables all the inputs instead of disabling them.
@andreeib: Did you check the working example via the link I posted? I just tested it in several browsers. Your issue must be elsewhere.
Yes I checked your example and it's working, I used the console and I disabled the inputs on this stackoverflow page but when I want to disable the inputs on my page I just end up enabling them all. Thank's for your quick answer.
@andreeib: Were you copy/pasting directly? Make sure you're not passing a string as the second argument to .attr(), it has to be a boolean (true).
0

Your piece of code seems ok, so the problem must be coming from another part of code. Put a break on the line where you disable and then run step by step.

Comments

0

Try this code. Onclick of button the textbox will be disabled.

 <script type="text/javascript" src="https://ajax.googleapis.com/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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.