Skip to content
the poor coder

if text exists in element using javascript

Poor Man
Published
Reading time
2 min read
On this page

If text exists in element using JavaScript#

As a web developer, I often come across situations where I need to check if a certain text exists in a particular HTML element. Fortunately, JavaScript provides us with various methods to accomplish this task.

The first method is by using the includes() method:#

The includes() method is a built-in JavaScript function that checks whether a string contains a specific substring or not. We can use this method to check if the text exists in an element's innerHTML.

Text
const element = document.getElementById("myElement");
const textToCheck = "Hello World";

if (element.innerHTML.includes(textToCheck)) {
  console.log("Text exists in the element.");
} else {
  console.log("Text does not exist in the element.");
}

In the above code snippet, we first select the HTML element that we want to check using its ID. We then define the text that we want to search for in the innerHTML of the element. Finally, we use the includes() method to check if the text exists in the element or not.

The second method is by using the indexOf() method:#

The indexOf() method returns the position of the first occurrence of a specified value in a string. We can use this method to check if the text exists in an element's innerHTML.

Text
const element = document.getElementById("myElement");
const textToCheck = "Hello World";

if (element.innerHTML.indexOf(textToCheck) !== -1) {
  console.log("Text exists in the element.");
} else {
  console.log("Text does not exist in the element.");
}

In the above code snippet, we first select the HTML element that we want to check using its ID. We then define the text that we want to search for in the innerHTML of the element. Finally, we use the indexOf() method to check if the text exists in the element or not by comparing it to -1.

The third method is by using regular expressions:#

Regular expressions provide a powerful way to search for patterns in strings. We can use them to check if a text exists in an element's innerHTML.

Text
const element = document.getElementById("myElement");
const textToCheck = /Hello World/;

if (textToCheck.test(element.innerHTML)) {
  console.log("Text exists in the element.");
} else {
  console.log("Text does not exist in the element.");
}

In the above code snippet, we first select the HTML element that we want to check using its ID. We then define the regular expression that we want to search for in the innerHTML of the element. Finally, we use the test() method to check if the regular expression matches the innerHTML of the element or not.

These are some of the ways in which you can check if a text exists in an HTML element using JavaScript. Choose the one that suits your needs best and implement it in your project.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC