MoreDeveloper271
MoreDeveloper271

Reputation: 15

Hide element if other class exist

i know how to do it with jquery, but how with clean javascript? Any help how to do this?

Here is my code

var element = document.getElementsByTagName('tagone')[0];

if(element !== null){ 
     document.getElementByClassName('classsix')[0].style.display = 'none';
}

Upvotes: 0

Views: 2873

Answers (4)

Prashant Zombade
Prashant Zombade

Reputation: 482

The javascript function is getElementsByClassName you missed 's'

function hide(){

    var y = document.getElementsByClassName("x");
    y[0].style.display = 'none'
} 

Here is the jsfiddle link.

Upvotes: 0

I tried this and it's working.

if (document.getElementsByTagName('tagone')[0] != null) {
    document.getElementsByTagName('classix')[0].style.display = 'none';
}

Upvotes: 0

Ved_Code_it
Ved_Code_it

Reputation: 774

your code is right. you can use like this also

var element = document.getElementsByTagName('tagone')[0];
 var element1= document.getElementsByClassName('classsix')[0];
   if(element && element1){
     element.style.display = 'none';
   }

Upvotes: 0

Dhananjai Pai
Dhananjai Pai

Reputation: 6015

You can use the hidden DOM attribute or create a new class called hide and use the logic accordingly.

var element = document.querySelector('tagone'); // returns only first match
if(element) { 
    document.querySelector('.classsix').setAttribute('hidden',true);
}

or you can create the class in css .hide { display: none; } and use document.querySelector('.classsix').classList.add('hide');

Upvotes: 1

Related Questions