Get multiple IDS, but perform different operations on each one

0

I have a function that gets multiple ids at once, but I have to do slightly different operations on each one, so I'm forced to create 3 variables for each different id. The idea is that if there is html content in id3, ids 1 and 2 should not be displayed, which leads to another repeat of style.display='none' . I tried to create a variable ( var s = s.style.display='none' ) to simplify the application, but I do not know how to apply it. My question is whether there is a way to reduce the amount of getElementById from 3 to 1, as well as style.display='none' ., Or anything else that reduces the number of repetitions. Thank you in advance for your attention.

function IDs(id1, id2, id3) {

var a = document.getElementById(id1);
var b = document.getElementById(id2);
var c = document.getElementById(id3);

var s = s.style.display='none'; // variável para aplicar estilo

if (a.innerHTML) {
c.style.display='none';
}

else {
a.style.display='none';
b.style.display='none';
 }
}
    
asked by anonymous 16.03.2015 / 15:35

1 answer

1

Jquery makes it easy to save code: You can call it any jquery action, such as an onclick, for example:

<script>
$(document).ready(function() {
    if($('#id1').html()) {
        $('#id3').hide();
    } else {
        $('#id1').hide();
        $('#id2').hide();
    }
});
</script>

However, to do with pure javascript, you would have to ecomonize code as follows:

<script>
function myHide(id) {
    document.getElementById(id).style.display='none';
}

if (document.getElementById('id1').innerHTML) {
    myHide('id3');
} else {
    myHide('id1');
    myHide('id2');
}
</script>

I hope to have helped, hug!

    
16.03.2015 / 16:45