Last active
May 16, 2024 12:17
-
-
Save dmhendricks/01e00354a802d11094a4bb7a54889bb2 to your computer and use it in GitHub Desktop.
Bind querySelector to dollar sign ($) to reference DOM elements without jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Bind querySelector to dollar sign ($) to easily reference DOM elements without jQuery | |
* Source: https://twitter.com/MrAhmadAwais/status/1113094169025708033 | |
*/ | |
// In case you're also loading jQuery | |
jQuery.noConflict(); | |
// Example - Change an element's background color | |
// Shorthand for: document.querySelector( '.selector' ).style.background = '#BADA55'; | |
const $ = document.querySelector.bind( document ); | |
$( '.selector' ).style.background = '#BADA55'; | |
// Or wait for DOM | |
(function($) { | |
$( '#element' ).innerHTML = 'DOM is loaded!'; | |
})( document.querySelector.bind( document ) ); |
Author
dmhendricks
commented
Jun 3, 2019
In case anyone sees this: using this alias causes Cypress to hang and fail when using .invoke(…)
. Too bad I couldn’t find a way to make both work at the same time!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment