0

I'm trying to check paragraphs to see if it contains a certain word (Apple), and if so, add that to a list.

<p>Orange</p>
<p>Grape</p>
<p>Apple</p>

<ul id=ulist>
</ul>

<script>
  var i;
  var x = document.getElementsByTagName("P");
  for (i = 0; i < x.length; i++) {
    document.getElementById("ulist").innerHTML = "<li>" + x[i].innerHTML + "<li>";
  }
</script>

I'm unsure how to check for words, but first I'm trying to go through each paragraph and add each word to the list, but it's only adding Apple. I don't understand why even though I set i=0.

Expected output:

- Apple

New to Javascript so I'd appreciate if the solution uses simple code even if it's long.

Kunj
  • 1,980
  • 2
  • 22
  • 34
catzen
  • 75
  • 8
  • You have a typo. `id=ulist` need quotes around the name. `
      `. Note that questions on this site will be closed if the problem was because of a typo
    – Rojo Mar 23 '21 at 12:32
  • 1
    @Rojo quotes are not needed https://html.spec.whatwg.org/multipage/syntax.html#unquoted – evolutionxbox Mar 23 '21 at 12:34
  • Note that you have an **unsafe** assignment to `innerHTML`. See [this question](https://stackoverflow.com/questions/45579400/best-way-to-purge-innerhtml-from-a-firefox-extension) for an alternative. – Rojo Mar 23 '21 at 12:34
  • @evolutionxbox huh. good to know – Rojo Mar 23 '21 at 12:36
  • @Rojo I still can't get over this. I started Front-end when XHTML was the new hotness – evolutionxbox Mar 23 '21 at 12:37

3 Answers3

2

Try this code:

var paragraphs = [...document.getElementsByTagName("P")];
var list = document.getElementById("ulist")

paragraphs.forEach(el => {
  if (el.innerText.toLowerCase().includes("apple")) {
    var li = document.createElement("LI")
    li.innerText = el.innerText
    list.appendChild(li)
  }
})
<p>Orange</p>
<p>Grape</p>
<p>Apple</p>
<p>Not Apple</p>

<ul id="ulist"></ul>

It checks if each <p> has the word apple in it, and if so, it dynamically adds it to the list.

If you want to check if it's Apple just by itself, use ==, and if you want to make the search case-sensitive, remove the .toLowerCase() and change the word apple to Apple.

If you want to add every element, simply remove the if condition.

Endothermic_Dragon
  • 1,147
  • 3
  • 13
0

You should use += instead of =. Also forgot to close the list element (should be </li>). Also, for better performance you can first generate string for all the items the update the DOM only once.

You can check the text of the current element in each iteration based on which you can create items.

Demo:

<p>Orange</p>
<p>Grape</p>
<p>Apple</p>

<ul id=ulist>
</ul>

<script>
  var i;
  var x = document.getElementsByTagName("p");
  var elString = "";
  for (i = 0; i < x.length; i++) {
    if(x[i].textContent.toLowerCase().includes('apple')){ // check if text contains the word
      elString += "<li>"+x[i].textContent+"</li>";
    }
  }
  document.getElementById("ulist").innerHTML = elString;
</script>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Using Document.querySelectorAll(), NodeList.prototype.forEach(),Document.getElementById(), ,Document.createElement() and RegExp.prototype.test().

const fruits = document.querySelectorAll("p"),
  lists = document.getElementById("ulist");

fruits.forEach((fruit) => {
  if (/apple/i.test(fruit.textContent)) {
    const list = document.createElement("li");
    list.textContent = fruit.textContent;
    lists.append(list);
  }
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Fruits</title>
  </head>
  <body>
    <p>Orange</p>
    <p>Grape</p>
    <p>Apple</p>
    <ul id="ulist"></ul>
  </body>
</html>