I wrote something that uses document.getElementById a lot so I was wondering if instead I should just make a function like:
function gid (id) { return document.getElementById(id); } and then just call it like:
alert(gid("test").innerText); I wrote something that uses document.getElementById a lot so I was wondering if instead I should just make a function like:
function gid (id) { return document.getElementById(id); } and then just call it like:
alert(gid("test").innerText); No, there's no reason not to. Typically most people use jQuery for stuff like this though.
.text() wasn't keeping the formatting like .innerText was.document.id().$("#test").get(innerText);$("#test").get(0).innerText-- $() doesn't return a DOM object but a jQuery object containing an array of objects matching the criteria of the selector; and the get() method as defined on jQuery objects returns the DOM object at the given index (whatever argument you send to get()). 0 is the first item in the array. $() always returns an array, even if only one item matches the selector. which is why you'd usually use .get(0).It's a good idea - it'll aid minification (if you chose to minify) and will save annoying spelling mistakes when you accidently type document.getElementsById. You can also shorten document.getElementsByTagName if you use it a lot.
I'm not saying that you should not provide a replacement function, but there are some downsides so you should make sure the balance is in favor of changing. A couple reasons not to shorten it: