admin管理员组文章数量:1392007
I am trying to use javaScript to determine if an element with a specific class name exists on an html page. The element in question is only sometimes loaded on the page.
When I use document.getElementsByClassName('element-in-question').innerHTML = "Hello"
It will work when the element exists, but when it doesn't exist, it will return as "cannot set property of innerHTML of undefined and the rest of the code will not run.
Is there a way to check if an element exists, and only modify it when it does without breaking the rest of the code? Thanks for the help
I am trying to use javaScript to determine if an element with a specific class name exists on an html page. The element in question is only sometimes loaded on the page.
When I use document.getElementsByClassName('element-in-question').innerHTML = "Hello"
It will work when the element exists, but when it doesn't exist, it will return as "cannot set property of innerHTML of undefined and the rest of the code will not run.
Is there a way to check if an element exists, and only modify it when it does without breaking the rest of the code? Thanks for the help
Share Improve this question asked Feb 1, 2020 at 18:12 mvernon27mvernon27 131 silver badge5 bronze badges 1-
You cannot set
.innerHTML
immediately onHTMLCollection
. You are facing another problem. Correct Syntex is :Array.from(document.getElementsByClassName('element-in-question')).forEach((v)=>{v.innerHTML = "hello";});
ordocument.getElementsByClassNams()[index].innerHTML = "..";
– Ritesh Khandekar Commented Feb 1, 2020 at 18:31
3 Answers
Reset to default 4You can also use document.querySelector
which will return the first element within the document if it exists, if not, it returns null
.
const targetElement = document.querySelector('.element-in-question');
if (targetElement) {
targetElement.innerText = 'Hi there!';
}
<div class="element-in-question"></div>
Tip: If you're just adding text consider using innerText
instead of innerHTML
.
Just wrap you code with if statement :
const elemts = document.getElementsByClassName('element-in-question');
if(elemts.length) {
// this actually need to be elemts[0].innerHTML
elemts.innerHTML = "Hello"
}
Note: document.getElementsByClassName
will return array/collection of elements so if you really know that there is no other elements keep using it otherwise switch to getElementById
.
as per documentation:
The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as an HTMLCollection object.
It's very simple with the condition IF
If you want to get elements by class, the function will return an array (a collection of all elements in the document with the specified class name), so you will check as following :
if (document.getElementsByClassName('class-in-question').length > 0) {
// Existed
}
If you want to get an element by specified id, the function will return an objet HTML with that id, so you will check as following :
if (document.getElementById('id-in-question')) {
// Existed
}
本文标签: How to check if an html element with a specific class exists in JavaScriptStack Overflow
版权声明:本文标题:How to check if an html element with a specific class exists in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768601a2624197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论