5 ms·
Ah yea you're right - I misspoke. Vanilla JS feels verbose, and jQuery feels more terse in regards to general syntax. --- document.querySelector('#sel').addEv
by peterangular 4y ago
Ah yea you're right - I misspoke. Vanilla JS feels verbose, and jQuery feels more terse in regards to general syntax.
---
document.querySelector('#sel').addEventListener('click', (e) => {})
Feels way more verbose than the following:
$('#sel').on('click', (e) => {})
My bad - apparently the coffee hadn't sunk in yet ha.
- 6510 4y agoOT note: sel.onclick=e=>{} works just fine. https://jsfiddle.net/39h5ynga/ https://jsfiddle.net/39h5ynga/
- joshstrange 4y agoBut only if you (and any libs you use) only plan on adding 1 click handler to an element. That's why I always opt for the addEventListener/on style vs setting the onclick/ondblclick/etc handlers.
- 6510 4y agotrue but I would hope the libs use the event. (one onclick is still allowed in combination) This approach seems the funniest: https://jsfiddle.net/eu60Lwv9/ https://jsfiddle.net/eu60Lwv9/
- strokirk 4y agoIt's even more verbose in a lot of common cases, actually. (Although the ? operator has made things better). --- 1. document.querySelectorAll(".sel").forEach((e) => e.addEventListener("click", (e) => {})) $('.sel').on('click', (e) => {}) 2. document.querySelector(".sel")?.addEventListener("click", (e) => {}) $('.sel').on('click', (e) => {}) 3. const e = document.createElement("div") e.className = "cls" e.setAttribute("title", "Title") e.textContent = "<Content>" const e = $('<div>').addClass("cls").attr("title", "Title").text("<Content>")