tags:

views:

25

answers:

1

i can't seem to add ids to the buttons in a jquery created dialog. i can select the buttons after the dialog is declared via selectors as follows:

var buttons = $("#dialog").siblings(".ui-dialog-buttonpane").find("button");

but when i try to do:

buttons[0].attr('id', 'someId');

i get:

TypeError: Object #<an HTMLButtonElement> has no method 'attr'

any suggestions? i don't see anything in the jquery docs that implies that attr shouldn't work on buttons

+6  A: 

Yes:

$(buttons[0]).attr('id', 'someId');

the attr() method is declared on jQuery objects, but buttons[0] gives you a standard DOM object (of type HTMLButtonElement in this case). jQuery objects are augmented arrays of DOM objects, so indexing into them always gives you the original DOM objects.

Since jQuery does not add methods to DOM objects themselves, you must first wrap the DOM object in a jQuery object to access these methods.

EDIT Then again… there is no need to use jQuery at all for this task.

buttons[0].id = 'someId';
Tomalak
+1 couldn't be simpler.
GenericTypeTea
makes perfect sense - great explanation. thanks very much
subrama6
@GenericTypeTea: Oh yes, there is a simpler way. See my edit.
Tomalak
@Tomalak: `-20` for your *obscene* amount of code then! :)
GenericTypeTea
@GenericTypeTea: It's too easy with jQuery to start thinking that the DOM does not exist anymore and everything needs to be done with and through jQuery.
Tomalak
@Tomalak - I know what you mean. I sometimes find myself totally forgetting there's such a thing as a DOM and JavaScript. jQuery is great, but can *verge* on evil.
GenericTypeTea