Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Kelas Premium!
Belajar Javascript untuk website
Gunakan kupon "mulaijs" untuk diskon 75 ribu!
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
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>
<div>Bantu jawab ya, mungkin bisa seperti ini :</div><pre><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Find A text</title> </head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <style> .black { background: black; color:white; font-weight:bold; } </style> <body> <div class="container"> <div class="card card-default"> <div class="card-header"> <h4 class="card-title">Text:</h4> </div> <div class="card-body"> <p id="my-text">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</p> </div> <div class="card-footer"> Find Words : <div class="input-group-addon"> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Type a keyword" id="keyword"> <button class="btn btn-primary" type="button" onclick="findWords()">Find!</button> </div> </div> </div> </div> </div> </body> </html></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) => {
if(word.includes(keyword.value)) {
newText += "<span class='black'>" + word + "</span> ";
} else {
newText += word + " ";
}
});
myTextEl.innerHTML = newText;
} else { alert('Please enter a keyword!'); } }</pre><div><br>Hasilnya bisa dicek disini ya : <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>
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><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Find A text</title> </head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <style> .black { background: black; color:white; font-weight:bold; } </style> <body> <div class="container"> <div class="card card-default"> <div class="card-header"> <h4 class="card-title">Text:</h4> </div> <div class="card-body"> <p id="my-text">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</p> </div> <div class="card-footer"> Find Words : <div class="input-group-addon"> <div class="input-group mb-3"> <input type="text" class="form-control" placeholder="Type a keyword" id="keyword"> <button class="btn btn-primary" type="button" onclick="findWords()">Find!</button> </div> </div> </div> </div> </div> </body> </html></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"), "<span class=black>$&</span>"); }</pre>