Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a easier way to make document.getElementById simplified?

Tags:

javascript

I am using code that requires me to write the following:

document.getElementById("id").innerHTML //+ whatever else

This code is getting used probably 20-30 times in my javascript with only the ID attribute changing. Is there a way I could write a very small function that replace this entire line of text with a more simplified version? Something like:

 El("id")

I have written probably 2-3 different functions to attempt to simply this, but none of them have worked.

var dom = {
    elem = function (id){
        return document.getElementById(id).innerHTML;
    }
}

dom.elem(testid) //this has not worked
like image 562
Shawn Avatar asked Sep 05 '25 03:09

Shawn


1 Answers

Try this:

function El(id) {
    return document.getElementById(id).innerHTML;
}

Then you can use:

El("elementId"); //will return document.getElementById("elementId").innerHTML
like image 121
joe_young Avatar answered Sep 07 '25 22:09

joe_young



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!