Skip to main content Accessibility Feedback

$.js

Get the first matching element in the DOM.

Works in all modern browsers, and at least back to IE9.

/*!
 * Get the first matching element in the DOM
 * (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
 * @param  {String} selector The element selector
 * @param  {Node}   parent   The parent to search in [optional]
 * @return {Node}            The element
 */
var $ = function (selector, parent) {
    return (parent ? parent : document).querySelector(selector);
};