19
AlgoRythm
79d

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
  • 2
    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?
  • 4
    document.querySelector
  • 2
    ID'd elements are already brought to the global scope?
  • 5
    getElementById is incredibly fast as well so I don't ever see the point in creating something else that just won't be as fast or reliable. It's tried and tested and there are alternatives if you feel it's lacking.

    Also, yeah, document.querySelector('[jsv]') will do the job.
  • 3
    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.
  • 5
    Offtopic: have you guys noticed the photos posted on devRant don't look so shit anymore?
  • 1
    @dontPanic

    That, sir, is a fantastic point that I had not noticed.
  • 0
    You can also use the shadow Dom to create your own element
  • 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
Your Job Suck?
Get a Better Job
Add Comment