choojs / choo

:steam_locomotive::train: - sturdy 4kb frontend framework
https://choo.io/
MIT License
6.78k stars 595 forks source link

Return Element via Specified Selector Question #633

Closed ohiosveryown closed 6 years ago

ohiosveryown commented 6 years ago

Expected behavior

document.querySelector("h1").classList.add('red');

Actual behavior

??

This is a super basic question but I'm very much a js / choo novice. How can I query select a tag and add a class with choo? What would be the best way to accomplish the expected behavior code?

yoshuawuyts commented 6 years ago

@ohiosveryown heya! - usually the best way to go about this is update the class="" attribute on an element, and then call emit('render'). If you only update the value using classList, you might lose the value on a re-render. E.g.

var clx = ''
app.route('/', () => {
  return html`<button class=${clx} onclick=${onclick}>click</button>`
  function onclick () {
    clx += '.my-class'
  }
})

Hope this is helpful!

ohiosveryown commented 6 years ago

Thanks for not only the help here, but the rest that you provide for everyone 🙌🏼.