What is the best way to traverse the DOM?

When you try to get an element is it better to do something like this:
var element = $('#someDivisionId .container-example .other-example').find('div');
or is it better to assign an id to that element when you are creating it and find it like this:
var element = $('#elementId');
By better I mean which way is faster and which way is a better practice?

Also in this line of thought why would you do:
$('#someDivisionId .container-example .other-example div').each(function(){});
when you…
What is the best way to traverse the DOM?

See original post by gandalf117

Leave a Reply