views:

870

answers:

2

So, I have JQuery dynamically generating controls. The input controls are called EnterLink + number of controls generated. On generation of a new input control I want to change the previously created input so it is disabled. Right now my code looks as so, it does not work.

if (rowCount > 0) {
                   var last = rowCount - 1;
                  $("#EnterLink" + last).disabled = true;
                  }

This looks like it should work, I checked the ids of the controls and that's exactly like they are formatted.

+7  A: 

Replace this:

$("#EnterLink" + last).disabled = true;

With this:

$("#EnterLink" + last).attr('disabled', true);

jQuery does not expose the regular DOM attributes directly through its $() function. What you have there is a wrapped set (that, granted, will only ever match 1 element but is a set nonetheless) that extends the elements and adds all the jQuery goodness to it.

If you wanted to get the native DOM element of #EnterLinkX, you would do this:

$("#EnterLink" + last)[0].disabled = true;

This works because the return value of $() is an array-like structure that contains the matched elements. [0] would contain the first (and in this case only) match. This would then give you access to stuff like innerHTML and such. 99% of the time, though, you are better off doing it the "jQuery way" as that's the whole point of the library. In this case, you would use it's attr function to set the attribute, and it's removeAttr function to remove the disabled value if you ever wanted to.

Paolo Bergantino
Gracias! 15characterlimitstinks
Sara Chipps
A perfect SO answer - complete, succinct, and correct! :)
Bobby Jack
+2  A: 

If you're trying to disable a button it's the following.

$("#EnterLink" + last).attr( "disabled", true ); or $("#EnterLink" + last).attr( "disabled", "disabled" );

http://docs.jquery.com/Alternative_ResourcesHas everything you need for jquery development.