Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing repetitive document.getElementById

So my question is: is there a way how to replace repetitive document.getElementById? as I have a lot of them and as many may agree, the code does not look nice like this.

Below is a sample of what I have in my code.

document.getElementById('FieldAddClient').hidden = true;
document.getElementById('FieldAddClient').disabled = true;
document.getElementById('AddingClientTitle').hidden = true;
document.getElementById('AddingClientDesc').hidden = true; 
document.getElementById('DuplicatingClientTitle').hidden = false; 
document.getElementById('DuplicatingClientDesc').hidden = false;
like image 388
jurgen.s Avatar asked Sep 20 '26 02:09

jurgen.s


1 Answers

Make 'em variables:

var my_el = document.getElementById('selector');

my_el.style.hidden = true;

Make a helper function:

function el_by_id(s)
{
    return document.getElementById(s);
}

el_by_id('selector').style.hidden = true;

Make a deeper helper function:

function style(s, p, v)
{
    document.getElementById(s)[p] = v;
}

style('selector', 'hidden', true);

or, use jQuery...

like image 175
Script47 Avatar answered Sep 21 '26 14:09

Script47



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!