anvd
anvd

Reputation: 4047

problem in code when update jquery

i have this code. Works well with jquery 1.5.2, now i updated to 1.6 and the the delete button doesn't work

here works well, but here, the button delete doesn't work

<script type="text/javascript">
    $(document).ready(function() {
        $('#btnAdd').click(function() {
            var num     = $('.clonedInput').length;
            var newNum  = new Number(num + 1);

            var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

            newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
            $('#input' + num).after(newElem);
            $('#btnDel').attr('disabled','');

            if (newNum == 5)
                $('#btnAdd').attr('disabled','disabled');
        });

        $('#btnDel').click(function() {
            var num = $('.clonedInput').length;

            $('#input' + num).remove();
            $('#btnAdd').attr('disabled','');

            if (num-1 == 1)
                $('#btnDel').attr('disabled','disabled');
        });

        $('#btnDel').attr('disabled','disabled');
    });
</script>

Any idea?

Upvotes: 0

Views: 111

Answers (2)

Tomalak
Tomalak

Reputation: 338248

The key point is that you have to use attr() correctly. disabled is a boolean attribute, to enable an element you must remove it, not set it to the empty string. See this: http://jsfiddle.net/zjcfN/

That prop() works is more of an accident than anything else. You can (and should!) continue to use attr() to work with HTML element attributes, and as of jQuery 1.6 you can additionally use prop() to work with DOM object properties.

The following works perfectly in jQuery 1.6:

// disable an element
$("input").attr("disabled", "disabled");
// enable an element
$("input").removeAttr("disabled");

Upvotes: 1

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

I think in jquery 1.6 attr() function has changed and in some cases you should use prop(). In 1.6.1 they re-introduced some changes for compatibilty. look here for prop() specification

Taken from jquery site:

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.

Upvotes: 3

Related Questions