0

I was wondering if there's a difference between the 2 options, knowing all my div's are with id's?
I took them all with querySelector... is it better to change to getElementById or is it equivalent?

document.querySelector("#div").value;

OR

document.getElementById("div").value;
gr3g
  • 2,684
  • 4
  • 24
  • 47
  • 5
    Better how? Performance-wise? – j08691 Aug 21 '14 at 16:05
  • Or code length? That answer is easy :) – Felix Kling Aug 21 '14 at 16:10
  • document.querySelector based ecmascript 5 standards it is works for html5 enabled browsers. but document.getElement by Id will works for legacy browsers like Internet Explorer 6. – sarath Aug 21 '14 at 16:10
  • Performance, and personal preference. I honestly think that `getElementById` and `getElementsByClassName` is prettier. – ndugger Aug 21 '14 at 16:13
  • 1
    @sarath: IE8 doesn't support many of the HTML5 features and still supports `document.querySelector`. Just saying that it's not restricted to browser fully supporting HTML5. – Felix Kling Aug 21 '14 at 16:13
  • 1
    http://jsperf.com/getelementbyid-vs-queryselector/11 – ndugger Aug 21 '14 at 16:14
  • More about compatibility on browsers and Performance. @NickDugger Thanks for this one. Post it as answer ;) – gr3g Aug 21 '14 at 17:14

0 Answers0