I have the following function written in jQuery which I would like to convert to javascript but I couldn’t find a proper way so far.
JavaScript
x
8
1
const word = document.getElementById("searchField").value;
2
const r = new RegExp("(" + word + ")", "ig");
3
$(".list-item").each(function (i) {
4
if ($(this).text().match(r)) {
5
6
}
7
});
8
I rewrote it this way:
JavaScript
1
10
10
1
const word = document.getElementById("searchField").value;
2
const r = new RegExp("(" + word + ")", "ig");
3
4
let pickComp = document.querySelectorAll('.list-item');
5
Array.from(pickComp).forEach((i) => {
6
if (//how can I rewrite the jQuery here?) {
7
8
}
9
})
10
Advertisement
Answer
JavaScript
1
10
10
1
const word = document.getElementById("searchField").value;
2
const r = new RegExp("(" + word + ")", "ig");
3
4
const pickComp = document.querySelectorAll('.list-item');
5
6
pickComp.forEach(item => {
7
if (item.innerHTML.match(r)) {
8
console.log("Match!!");
9
}
10
})
JavaScript
1
5
1
<p class="list-item">abc</p>
2
<p class="list-item">abc</p>
3
<p class="list-item">abc</p>
4
<p class="list-item">abc</p>
5
<input id="searchField" value="abc">