How to check if ul has li with the given text ?

30 Jan, 2020
Method 1: Using the each() method on the list to check the innerText property
Each of the list elements of the unordered list is first selected using a jQuery selector. The each() method is used on this list to iterate through it. This method has a callback function that returns the current index and the element of the iteration.
The text inside the returned element is checked for the innerText property to see if it matches the text required. A successful match means that the text inside the selected unordered list has the required text.
Syntax:
let found = false; $("#list li").each((id, elem) => {  if (elem.innerText == requiredText) {    found = true;  }}); return found;
Example:
<!DOCTYPE html><html>  <head>    <title>        Check if ul has li with a specific text in jQuery.    </title></head>  <body>    <h1 style="color: green">    GeeksforGeeks  </h1>    <b>    How to check if ul has li with    a specific text in jQuery?  </b>    <ul id="list">        <li>GeeksforGeeks</li>        <li>Computer</li>        <li>Science</li>        <li>Portal</li>    </ul>    <p>        The li elements contain the text "Computer":      <span class="output">    </span>    </p>    <p>        The li elements contain the text "Python":      <span class="output2">    </span>    </p>    <button onclick="runChecks()">        Check for the text    </button>    <script src="https://code.jquery.com/jquery-3.4.1.min.js">    </script>    <script>        function checkforText(requiredText) {            let found = false;              $("#list li").each((id, elem) => {                if (elem.innerText == requiredText) {                    found = true;                }            });              return found;        }          function runChecks() {            ans1 = checkforText('Computer');            document.querySelector(".output").textContent = ans1;              ans2 = checkforText('Python');            document.querySelector(".output2").textContent = ans2;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
Method 2: Using the contains() selectorThe contains() selector is used to select elements that have the text matching to the given one. This text could be present in any of the element’s descendants or the element itself. It takes one parameter that is the case-sensitive text to be matched.
The contains() selector is used along with the selector used for selecting the list elements. If no element is selected, that is, if the given text is not present, the number of elements returned would be 0. The number of returned elements could be checked with the length property and used to verify if the list contains the specified text.
Syntax:
let found = false; selector = `#list :contains('${requiredText}')`selectedList = $(selector); if (selectedList.length) {  found = true;}return found;
Example:
<!DOCTYPE html><html>  <head>    <title>        Check if ul has li with a specific text in jQuery.    </title></head>  <body>    <h1 style="color: green">    GeeksforGeeks  </h1>    <b>    How to check if ul has li with    a specific text in jQuery?  </b>    <ul id="list">        <li>GeeksforGeeks</li>        <li>Computer</li>        <li>Science</li>        <li>Portal</li>    </ul>    <p>        The li elements contain the text "Computer":      <span class="output">    </span>    </p>    <p>        The li elements contain the text "Python":      <span class="output2">    </span>    </p>    <button onclick="runChecks()">        Check for the text    </button>    <script src="https://code.jquery.com/jquery-3.4.1.min.js">    </script>    <script>        function checkforText(requiredText) {            let found = false;              selector                = `#list :contains('${requiredText}')`            selectedList = $(selector);              if (selectedList.length) {                found = true;            }            return found;        }          function runChecks() {            ans1 = checkforText('Computer');            document.querySelector(".output").textContent = ans1;              ans2 = checkforText('Python');            document.querySelector(".output2").textContent = ans2;        }    </script></body>  </html>
Output:
Before clicking the button:
After clicking the button:
jQuery-Misc
Picked
JQuery
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to get the value in an input text box using jQuery ?
How to prevent Body from scrolling when a modal is opened using jQuery ?
jQuery | ajax() Method
jQuery | removeAttr() with Examples
jQuery | parent() & parents() with Examples
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?