Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I have heard that querySelector and querySelectorAll are new methods to select DOM elements. How do they compare to the older methods, getElementById and getElementsByClassName in terms of performance and browser support?
How does the performance compare to using jQuery's query selector?
What is the difference between queryselector and getElementById?
When should we use queryselector instead of getelementbyid? Is there Any example which is not possible using getElementById?
"Better" is subjective.
querySelector is the newer feature.
getElementById is better supported than querySelector.
querySelector is better supported than getElementsByClassName but querySelector gives you a static node list while getElementsByClassName gives you a live node list.
querySelector lets you find elements with rules that can't be expressed with getElementById and getElementsByClassName
You need to pick the appropriate tool for any given task.
(In the above, for querySelector read querySelector / querySelectorAll).
The functions getElementById and getElementsByClassName are very specific, while querySelector and querySelectorAll are more elaborate. My guess is that they will actually have a worse performance.
Also, you need to check for the support of each function in the browsers you are targetting. The newer it is, the higher probability of lack of support or the function being "buggy".
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
I have heard that querySelector and querySelectorAll are new methods to select DOM elements. How do they compare to the older methods, getElementById and getElementsByClassName in terms of performance and browser support?
How does the performance compare to using jQuery's query selector?
What is the difference between queryselector and getElementById?
When should we use queryselector instead of getelementbyid? Is there Any example which is not possible using getElementById?
"Better" is subjective.
querySelector is the newer feature.
getElementById is better supported than querySelector.
querySelector is better supported than getElementsByClassName but querySelector gives you a static node list while getElementsByClassName gives you a live node list.
querySelector lets you find elements with rules that can't be expressed with getElementById and getElementsByClassName
You need to pick the appropriate tool for any given task.
(In the above, for querySelector read querySelector / querySelectorAll).
The functions getElementById and getElementsByClassName are very specific, while querySelector and querySelectorAll are more elaborate. My guess is that they will actually have a worse performance.
Also, you need to check for the support of each function in the browsers you are targetting. The newer it is, the higher probability of lack of support or the function being "buggy".
This question already has answers here:
Lightweight alternative to jQuery for class / id selecting
(6 answers)
Closed 8 years ago.
I have this jQuery selector:
$('#stuffElements').find('[data-markerlayer="layer1"]');
I have a ton of selectors similar to this one, and I want to optimize my script as much as possible, as rewriting most selectors requires only minimal effort on my part.
Disregarding the discussion whether this is useful, is it possible to write the above selector out in pure JavaScript?
document.querySelectorAll('#stuffElements [data-markerlayer="layer1"]')
or to make it more efficient:
var holder = document.getElementById('#stuffElements'); // cache parent node
holder.querySelectorAll('[data-markerlayer="layer1"]'); // finds inside it
querySelectorAll doesn't work in IE7 if that bothers you.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i mean i wanna iterate manually using a for-loop or something. but this piece of code i came up with seems to be not working. i like combining javascript with jquery since jquery is not my cup of tea for major projects. i don't know much jquery either, i would say I'm beginning to learn, though. how do you iterate over a nodelist in jquery is the question i have for all those jquery fans this time. is it similar to the javascript way? anyway this is what i have come up with (the code of a beginner).
$("sn"[i]).fadeIn();
$("sn"[i]) the part which failed, according to google chrome.
try this:
$("sn[" + i + "]").fadeIn();
I think you mean that "sn" is the selector for the nodes, in that case:
$("sn").fadeIn();
This works on all the elements that match the selector, jQuery will do the iteration. However if you want to select all elements that have the 'sn' class you should prefix the selector with a . like so: ".sn"
if you want to loop manually try:
$(".sn").each(function(i) {
$(this) // do some magic with the individual element here
});
See more on iterating with each here:
https://api.jquery.com/each/
Assuming sn is a variable containing the node list, you are probably looking for
$(sn[i])
or
sn.eq(i)
if sn is already a jQuery object.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
should I replace document.getElementById or so by document.querySelector?
any difference?
Will you recommend me to use querySelector?
When you are selecting on ids anyway, use getElementById as that’s a lot more efficient than using querySelector on an id selector. The latter runs the whole CSS selector parsing, while the former can just take the ID and get the element with that ID directly.
Of course, when selecting based on other criteria than the element’s id, querySelector (and querySelectorAll) obviously has its place.
(The obligatory benchmark to prove this claim, although I do want to note that benchmarks are not everything, and the difference probably won’t make much difference in an actual application.)
Both serves same purpose in your case. But getElementById is most proven approach. However, if you are not concerned about legacy browsers then querySelector will also suffice.
Enjoy!
If querySelector is available in the browser that your users use. Then you can use it. It is nice to not have to litter your html with ids. Selecting using css selectors is extremely flexible.
Performance is a red herring. My shitty old laptop can manage 3 million selections a second...
The real problem is compatibility. How are you going to deal with browsers that don't have it available? Do you care? You probably have to care.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am writing up some code in Javascript/JQuery that involves some confusing DOM operations, or at least they are to me because I'm relatively inexperienced.
I try to help myself by doing a couple of console.log()s, but the thing is if you just log a DOM element you get useless information, mostly it's object Object.
I was wondering what the most usefull generic attributes of a HTML element are so that I could easily follow what the script is doing?
I was wondering what the most usefull generic attributes of a HTML element are so that I could easily follow what the script is doing?
Don't use logging for debugging. Use a debugger for debugging. Even IE (8 and up) has a debugger built in. If you want to know what the code is doing, there's nothing that substitutes for stepping through the code in the debugger and examining your various variables and such as you go.
But answering your specific question, I'd say I'd want to see the DOM element's tagName, className (e.g, class[es]), and id (if any).
I would use chrome when inspecting the console log, the objects actually come through so you can expand and see all of their properties. Keep in mind some of the older browsers, IE8 or 7, do not support console.log and it will throw a javascript error.