
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Highlight a text, every time page loads with JavaScript
To highlight a text, every time page loads, use the for loop and the concept of −
<span class=”yourColorName”>
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <style> .yellow { color: yellow; } .blue { color: blue; } </style> </head> <body> <h1 id="pageLoads">My yellow Words,your blue Words</h1> <script> var ids = document.getElementById("pageLoads"); var loadColorWords = ids.innerHTML.split(" "); for(var i = 0; i < loadColorWords.length; i++) { if(loadColorWords[i] == "blue") { loadColorWords[i] = "<span class='blue'>" + loadColorWords[i] + "</span>"; } if(loadColorWords[i] == "yellow") { loadColorWords[i] = "<span class='yellow'>" + loadColorWords[i] + "</span>"; } } ids.innerHTML = loadColorWords.join(" "); </script> </body> </html>
To run the above program, save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS code editor.
Output
- Related Articles
- How page load time affects with JavaScript?
- How to highlight text in a tkinter Text widget?
- Remove any text not inside element tag on a web page with JavaScript?
- JavaScript code to find the coordinates of every link in a page
- Finding text on page with Selenium 2
- JavaScript code to de-select text on HTML page.
- How to highlight the current line of a Text widget in Tkinter?
- I want to highlight all the text in the Java Swing Control Text Pane
- How to specify that the element should automatically get focus when the page loads in HTML?
- How to specify that an element should be pre-selected when the page loads in HTML?
- How to specify that an option should be pre-selected when the page loads in HTML?
- How to highlight text inside a plot created by ggplot2 using a box in R?
- Place autofocus in the text box when a page gets loaded without JavaScript support in HTML?
- Calculate text width with JavaScript
- Creating ‘Copy to Clipboard’ feature on a web page with JavaScript

Advertisements