How to check if an html element with a specific class exists in JavaScript? - Stack Overflow

I am trying to use javaScript to determine if an element with a specific class name exists on an html p

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 on HTMLCollection. You are facing another problem. Correct Syntex is : Array.from(document.getElementsByClassName('element-in-question')).forEach((v)=>{v.innerHTML = "hello";}); or document.getElementsByClassNams()[index].innerHTML = ".."; – Ritesh Khandekar Commented Feb 1, 2020 at 18:31
Add a ment  | 

3 Answers 3

Reset to default 4

You 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
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744768602a4592617.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信