17

I wrote this and wonder if it is actually useful. This is a function that eliminates the need for document.getElementById. In the HTML you just set an attribute (jsv) to some value and this scoops that up and puts it in the global scope for you to use.

Neat or shit?

Comments
  • 1
    i was not aware to be able to set custom properties. that's cool!
    but what is wrong about
    function el(v){return document.getElementById(v);}
    or are you running out of ids for single elements? or just a proof of concept?
  • 3
    document.querySelector
  • 1
    ID'd elements are already brought to the global scope?
  • 2
    element.querySelector('[jsv]') is possible, too. It's not only available on the document.

    And if you really need custom attributes please use data-jsv. As it is intended do be done.
  • 4
    Offtopic: have you guys noticed the photos posted on devRant don't look so shit anymore?
  • 0
    This looks like data-dojo-attach-point, if you ever used dojo. Btw it's a good way to structure a document, imo
  • 0
    It's neat but why are you using IIFE ?
  • 1
    This doesn't work for elements created dynamically (after the page loads).

    It also creates memory leaks: when elements removed, they still referenced therefore cannot be deallocated.
  • 0
    @nam17887 Dynamic elements will be created in JS and therefore there is no need for this system in that case.

    Hand written HTML elements shouldn't be so plentiful that they fill up memory. And you can in fact delete the variables if you are so inclined.
  • 1
    @nitwhiz I would use the data prefix if it wasn't ugly. The querySelector solution looks very nice
  • 1
    @Charon92 I'm eliminating document.getElementById because I hate writing stuff in HTML and then repeating myself in JS. I just want the variables to be there.

    Since the function only runs once on page load I am not concerned about speed.
  • 1
    @erroronline1 I hate writing something in HTML then essentially repeating myself in JS
Add Comment