Skip to main content

No need for jQuery (basic solution)

if(document.querySelector('.a-class')) {
  // do something
}

Much more performant option below  (notice the lack of a dot before a-classa-class).

if(document.getElementsByClassName('a-class')[0]) {
  // do something
}

querySelectorquerySelector uses a proper matching engine like $()$() (sizzle) in jQuery and uses more computing power but in 99% of cases will do just fine. The second option is more explicit and tells the code exactly what to do. It's much faster according to jsperfJSBench https://jsperf.com/getelementsbyclassname-vs-queryselectorall/25https://jsbench.me/65l2up3t8i

No need for jQuery (basic solution)

if(document.querySelector('.a-class')) {
  // do something
}

Much more performant option below(notice lack of a dot before a-class).

if(document.getElementsByClassName('a-class')[0]) {
  // do something
}

querySelector uses a proper matching engine like $() (sizzle) in jQuery and uses more computing power but in 99% cases will do just fine. The second option is more explicit and tells the code exactly what to do. It's much faster according to jsperf https://jsperf.com/getelementsbyclassname-vs-queryselectorall/25

No need for jQuery (basic solution)

if(document.querySelector('.a-class')) {
  // do something
}

Much more performant option below  (notice the lack of a dot before a-class).

if(document.getElementsByClassName('a-class')[0]) {
  // do something
}

querySelector uses a proper matching engine like $() (sizzle) in jQuery and uses more computing power but in 99% of cases will do just fine. The second option is more explicit and tells the code exactly what to do. It's much faster according to JSBench https://jsbench.me/65l2up3t8i

much faster version
Source Link
Pawel
  • 17.7k
  • 5
  • 76
  • 75

No need for jQuery (basic solution)

if(document.querySelector('.a-class')) {
  // do something
}

Much more performant option below(notice lack of a dot before a-class).

if(document.getElementsByClassName('a-class')[0]) {
  // do something
}

querySelector uses a proper matching engine like $() (sizzle) in jQuery and uses more computing power but in 99% cases will do just fine. The second option is more explicit and tells the code exactly what to do. It's much faster according to jsperf https://jsperf.com/getelementsbyclassname-vs-queryselectorall/25

No need for jQuery

if(document.querySelector('.a-class')) {
  // do something
}

No need for jQuery (basic solution)

if(document.querySelector('.a-class')) {
  // do something
}

Much more performant option below(notice lack of a dot before a-class).

if(document.getElementsByClassName('a-class')[0]) {
  // do something
}

querySelector uses a proper matching engine like $() (sizzle) in jQuery and uses more computing power but in 99% cases will do just fine. The second option is more explicit and tells the code exactly what to do. It's much faster according to jsperf https://jsperf.com/getelementsbyclassname-vs-queryselectorall/25

Source Link
Pawel
  • 17.7k
  • 5
  • 76
  • 75

No need for jQuery

if(document.querySelector('.a-class')) {
  // do something
}