Skip to content

04. Get First Element by Class Name (dgCNz.js)

Pimp Trizkit edited this page Mar 7, 2018 · 3 revisions
<< Previous        Back to Table of Contents        Next >>

    "I always roll first class... i think." - PT

This is just an alias function to shorten other JS code.

"dgCNz" alias for "document.getElementsByClassName (zero index)".

This will search inside document for the first element that has a className equal to the string specified. And then return that element or undefined.

Code:

const dgCNz=(c)=>document.getElementsByClassName(c)[0]||false;

Usage:

let e = dgCNz("myClassName");

Return:

The first HTML Element, from inside document, that has className equal to the parameter specified. NOTE: If no such element exists, false will be returned.

Params:

dgCN(c)

c = < ClassName String > * REQUIRED

  • A string to search in document for the first occurance of an element with the same className.
  • This function uses document.getElementsByClassName(), and is just an alias, except that it converts the undefined to a false upon return if the ClassName was not found.