I have two functions that generate DOM elements. They both attempt to create the same container element. (I’d prefer not to enforce a specific run order for these functions, or tightly couple them to the side-effect of creating this element). I’ve been trying to discover a method that would get an element if it exists, or create a single instance of it if it doesn’t, and return a jQuery object containing that element or all the matched elements if they already exist.
I haven’t found this to exist in searching through jQuery documentation, but I thought I’d ask here to see if anyone else has encountered a similar problem and has suggestions.
You can use this poorly written code:
Or make it a bit cleaner: