Menghitamkan kata yang di cari menggunakan javascript [closed]

Halo, cara menghitamkan kata yang dicari menggunakan javascript gimana ya?

Misal-nya teks nya :

Ngoding di Sekolah Koding

User mencari :

Koding

Hasil :

Ngoding di Sekolah Koding

avatar syahid246
@syahid246

70 Kontribusi 160 Poin

Diperbarui 3 tahun yang lalu

3 Jawaban:

<div>Range boosters are very good for every type of domain you have, and it only consists of improving your interaction algorithm. You must increase the popularity of your website using legendary SEO techniques that solve your life in weeks. You can know and use a good number of techniques at no cost or hire the most profitable one.<br>Find Out How Quickly You Can Rank High In Your Business<br><br><a href="https://www.thearticlereview.com/">https://www.thearticlereview.com/</a></div>

avatar thearticlereview
@thearticlereview

1 Kontribusi 0 Poin

Dipost 3 tahun yang lalu

<div>Bantu jawab ya, mungkin bisa seperti ini :</div><pre>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Find A text&lt;/title&gt; &lt;/head&gt; &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"&gt; &lt;style&gt; .black { background: black; color:white; font-weight:bold; } &lt;/style&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;div class="card card-default"&gt; &lt;div class="card-header"&gt; &lt;h4 class="card-title"&gt;Text:&lt;/h4&gt; &lt;/div&gt; &lt;div class="card-body"&gt; &lt;p id="my-text"&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta labore, commodi suscipit architecto non impedit fuga perferendis deleniti, nemo. Voluptatum est similique necessitatibus quisquam voluptas nostrum nobis nulla doloribus, id&lt;/p&gt; &lt;/div&gt; &lt;div class="card-footer"&gt; Find Words : &lt;div class="input-group-addon"&gt; &lt;div class="input-group mb-3"&gt; &lt;input type="text" class="form-control" placeholder="Type a keyword" id="keyword"&gt; &lt;button class="btn btn-primary" type="button" onclick="findWords()"&gt;Find!&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><div><br>Javascript :</div><pre>const myTextEl = document.getElementById("my-text"); const myText = myTextEl.textContent; const keyword = document.getElementById("keyword");

function findWords() { //Validasi if(keyword.value !== '') { let found = myText.includes(keyword.value);
let indexWord = myText.indexOf(keyword.value); //console.log(found); //console.log(indexWord); let newText = ""; myText.split(" ").forEach((word, index) =&gt; { if(word.includes(keyword.value)) { newText += "&lt;span class='black'&gt;" + word + "&lt;/span&gt; "; } else { newText += word + " "; } });

myTextEl.innerHTML = newText;

} else { alert('Please enter a keyword!'); } }</pre><div><br>Hasilnya bisa dicek disini ya :&nbsp;<a href="https://jsbin.com/dubiyap/edit?html,js,console,output">https://jsbin.com/dubiyap/edit?html,js,console,output</a><br><br>Semoga berhasil.</div>

avatar ahanafi
@ahanafi

815 Kontribusi 554 Poin

Dipost 3 tahun yang lalu

Jawaban Terpilih

<div>Maaf, itukan sudah ada tulisan "[closed]", berarti saya sudah bertemu dengan jawabannya...<br><br>Kalau mau tahu bisa seperti ini :</div><pre>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Find A text&lt;/title&gt; &lt;/head&gt; &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"&gt; &lt;style&gt; .black { background: black; color:white; font-weight:bold; } &lt;/style&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;div class="card card-default"&gt; &lt;div class="card-header"&gt; &lt;h4 class="card-title"&gt;Text:&lt;/h4&gt; &lt;/div&gt; &lt;div class="card-body"&gt; &lt;p id="my-text"&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Soluta labore, commodi suscipit architecto non impedit fuga perferendis deleniti, nemo. Voluptatum est similique necessitatibus quisquam voluptas nostrum nobis nulla doloribus, id&lt;/p&gt; &lt;/div&gt; &lt;div class="card-footer"&gt; Find Words : &lt;div class="input-group-addon"&gt; &lt;div class="input-group mb-3"&gt; &lt;input type="text" class="form-control" placeholder="Type a keyword" id="keyword"&gt; &lt;button class="btn btn-primary" type="button" onclick="findWords()"&gt;Find!&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><div><br>Javascript :<br><br></div><pre>const myTextEl = document.getElementById("my-text"); const myText = myTextEl.textContent; const keyword = document.getElementById("keyword");

function findWords() { myTextEl.innerHTML = myText.replace(new RegExp(keyword.value, "gi"), "&lt;span class=black&gt;$&amp;&lt;/span&gt;"); }</pre>

avatar syahid246
@syahid246

70 Kontribusi 160 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban