Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript highlight words

<script>
function highlight(text) {
  var inputText = document.getElementById("inputText");
  var innerHTML = inputText.innerHTML;
  var index = innerHTML.indexOf(text);
  if (index >= 0) { 
   innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+text.length) + "</span>" + innerHTML.substring(index + text.length);
   inputText.innerHTML = innerHTML;
  }
}
</script>


<style>
.highlight {
  background-color: yellow;
}
</style>


<button onclick="highlight('fox')">Highlight</button>

<div id="inputText">
  The fox went over the fence
</div>

// adpted by: iury.landin
Comment

PREVIOUS NEXT
Code Example
Javascript :: tinymce update textarea value using jquery 
Javascript :: how to increment counter button click in js 
Javascript :: counter in javascript 
Javascript :: avascript regex email 
Javascript :: iife javascript 
Javascript :: react script 
Javascript :: mssql node js documentation 
Javascript :: convert date and time into epoch javascript 
Javascript :: javaScript setSeconds() Method 
Javascript :: redux extension 
Javascript :: js read xml file 
Javascript :: formdata append not working 
Javascript :: how to make a div appear onclick 
Javascript :: get element of an array inside another array 
Javascript :: moment get iso week number 
Javascript :: get the text of a tag 
Javascript :: tick.json code 
Javascript :: display console log in HTML 
Javascript :: bubble sort js 
Javascript :: jquery select first matching element 
Javascript :: js get file location 
Javascript :: formgroup is not property of form angular 
Javascript :: check if number is decimal or integer js 
Javascript :: numbered occurences in regex 
Javascript :: object flatten js 
Javascript :: how to get an array from another script in js 
Javascript :: how to print in javascript 
Javascript :: how to push items in array in javascript 
Javascript :: what is status 400 in react 
Javascript :: javascript sort map by value 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =