Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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>
    <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:
    each-method-before
  • After clicking the button:
    each-method-after

Method 2: Using the contains() selector
The 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>
    <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:
    contains-before
  • After clicking the button:
    contains-after


Last Updated : 30 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads