0

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?

anvd
  • 3,997
  • 19
  • 65
  • 126

2 Answers2

3

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.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • http://blog.jquery.com/2011/05/03/jquery-16-released/ It is the first point mentioned in that blog post. – Gazler May 29 '11 at 15:57
  • This is plain wrong. You do **not** *have to* use `prop()`. You make it sound as if `attr()` would be deprecated. – Tomalak May 29 '11 at 15:59
  • 1
    @Tomalak you are right i changed slightly my answer so that it's not confusing! I didn't mean that prop is discontinued, i just meant that in 1.6 there are cases when attr() doesn't work as it did in 1.5.2 – Nicola Peluchetti May 29 '11 at 16:02
1

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");
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • i tested with $('#btnDel').removeAttr('disabled','disabled'); Doesn't work – anvd May 29 '11 at 18:05
  • @Fel Where did I say `removeAttr('disabled','disabled')`? Also, is the jsFiddle working for you? – Tomalak May 29 '11 at 18:35
  • but..."In the majority of cases, prop() does what attr() used to do. Replacing calls to attr() with prop() in your code will generally work." - relevant question here: http://stackoverflow.com/questions/5874652/prop-vs-attr – anvd May 30 '11 at 03:42