user3835327
user3835327

Reputation: 1204

Is it possible to hide all the element tag by using only 1 id name?

I'm trying to hide certain tags by using one ID element, but seem like it only hide the first tag with the ID element that I used.

DEMO : http://jsfiddle.net/mgm3j5cd/

How can i solve this issue? I wanted to hide the tag only with the ID element that I've declared. Appreciated for helps

Upvotes: 0

Views: 177

Answers (2)

Bien Holmes
Bien Holmes

Reputation: 3

The simplest solution should be to assign 'class' attribute to certain elements you want to hide, like :

.XXXX
{
   display:none;
}

Perhaps, you want to specify some elements hidden with id, like :

#id1 , #id2
{
   display:none;
}

or

div#id1 , div#id2 //more accurate
{
   display:none;
}

but, unfortunately, you can't hide elements you want by using one ID.

Upvotes: 0

iglvzx
iglvzx

Reputation: 508

You have this tagged as CSS, so the following CSS in your page's stylesheet will work:

#hide {
    display: none;
}

Edit:

If you must only use JavaScript, you can do the following. Keep in mind that your document is already technically invalid by having multiple elements with the same ID, so this approach may not work in every browser. (I tested with Firefox 32).

Working JSFiddle: http://jsfiddle.net/88yw7LL9/2/

function hideByID(string) {
    var element = document.getElementById(string); // get first matching element
    var array = [];
    while(element) {
        array.push(element);
        element.id = string + '-processed'; // change ID so next call gets the next matching element
        element = document.getElementById(string);
    }

    for(var i = 0; i < array.length; i++) {
        array[i].id = string; // revert ID to previous state
        array[i].style.display="none"; // hide
    }
}

hideByID('hide');

Upvotes: 1

Related Questions