Skip to content Skip to sidebar Skip to footer

Removing Html Element By Id

I am using the following code to remove an element from the DOM tree: function onItemDeleted(name) { $('#' + name).remove(); } Would t

Solution 1:

jQuery optimises for ID searches. So, $("#" + name) is effectively the same as $(document.getElementById(name)). From the source, line 120 (1.4):

// HANDLE: $("#id")
} else {
    elem = document.getElementById( match[2] );

Solution 2:

The difference in performance would likely be negligible.

Solution 3:

I guess that when you find elements by ID, the lookup time should be O(1) since there can be only one element with that ID.

Post a Comment for "Removing Html Element By Id"