Reputation: 35096
I am right now recursively descending my DOM in javascript looking for a specific (custom) attribute. This works, but I'm just curious if this is inefficient versus built in query functionality in JavaScript / JQuery.
function createTooltipsRecurse(elem) {
if (!elem.getAttribute) return;
if (elem.getAttribute('tooltip')) {
$(elem).hover(
function (event) {
$('#tt').html(elem.getAttribute('tooltip'));
$('#tt').css('left',(event.pageX + 10) + 'px');
$('#tt').css('top',event.pageY + 'px');
$('#tt').show();
},
function (event) {
$('#tt').hide();
});
}
for (var i = 0; i < elem.childNodes.length; i++) {
createTooltipsRecurse(elem.childNodes[i]);
}
}
Thanks!
Upvotes: 0
Views: 54
Reputation: 5960
This is now fully native (therefore faster) and with very convenient JQuery/CSS selectors:
document.querySelectorAll('*[your-attribute]');
This will return the list of the nodes with your-attribute
.
Hope this helps!
Upvotes: 3