Reputation: 15
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
Reputation: 482
The javascript function is getElementsByClassName you missed 's'
function hide(){
var y = document.getElementsByClassName("x");
y[0].style.display = 'none'
}
Upvotes: 0
Reputation: 54
I tried this and it's working.
if (document.getElementsByTagName('tagone')[0] != null) {
document.getElementsByTagName('classix')[0].style.display = 'none';
}
Upvotes: 0
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
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