jQuery Highlight Text in Div

In this tutorial we will learn how to Highlight Text in Div with jQuery. jQuery addClass() method and click() method can be used along with simple CSS to highlight the div text on click.

jQuery addClass() Method

jQuery addClass() Method adds the CSS class to the selected HTML element.

In this example addClass() method is used to add the class to the div which will highlight it's text.

jQuery click() Method

jQuery click() Method binds the click event with the HTML element or it triggers the click event for the selected HTML element.

In this example click() Method is used to execute a function which is going to highlight the text of div.

HTML Code

HTML Code is given below, in this code we have a main div with dummy text inside and a button tag.

CSS is also used to define a class to change the background color of the div which will highlight the text.

<!DOCTYPE html>
<html>
<head>
<title>jQuery Highlight Text in Div</title>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<style>
.highlight
{   
background-color: yellow; 
}
#div
{
padding: 5px;
}
</style>    
</head>
<body>
    
<div id="div">Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an 
unknown printer took a galley of type and scrambled it to make a type specimen book. It has 
survived not only five centuries, but also the leap into electronic typesetting, remaining 
essentially unchanged. It was popularized in the 1960s with the release of Letterset sheets 
containing Lorem Ipsum passages, and more recently with desktop publishing software like 
Aldus PageMaker including versions of Lorem Ipsum.
</div>    
<button id="btn">Highlight</button>    
<script src="script.js"></script>    
</body>
</html>

JQuery Code

JQuery Code is given below, the code is executed whenever button is clicked. This is done using jQuery click() method.

Then addClass() method is used to add the class named highlight to the div.

<script>
$(document).ready(function(){
   $('#btn').click(function(){
    $('#div').addClass('highlight');   
   }); 
});
</script>

Demo

Video Tutorial

Watch video tutorial on How To Highlight Text in Div using jQuery.

Check radio button Dynamically with jQuery Change Background Color on hover with jQuery Hide and Show div using jQuery Get value of input element with jQuery Remove last child of parent element with jQuery Add li tag to the end of list jQuery Delete first row of Table with jQuery Delete last row of Table with jQuery Add first list item using jQuery jQuery Get id of Button jQuery toggle show hide on click Disable Button after Click with jQuery Get Button Text with jQuery jQuery Dynamic li Click Event How To Remove all Spaces in String with jQuery Get Selected file name in jQuery Remove Last Option of Select tag with jQuery Remove First Option from Select tag in jQuery jQuery Capitalize First Letter of String jQuery Check input value length jQuery Count all elements with same Class jQuery Get Checked Radio Button Value By Class jQuery Highlight Text in Div Get Checked Radio Button Value By Name in jQuery Get Checked Radio Button Value By id jQuery