Locate specific text within the content and apply an underline to it

Is there a way to locate specific text on my website and apply an underline to it?

Suppose I have some text displayed and I wish to identify all instances of the word hello and underline them.

Here is my attempt at the code:

$( "body:contains('hello')" ).css( "text-decoration", "underline" );

However, this approach seems to be underlining the entire line instead of just the individual word.

Answer №1

Give this a try.

Markup

<p>For instance, there is some text on the website and you want to highlight all occurrences of the word hello.</p>

Javascript

$('p').html( $('p').html().replace(/hello/g, '<strong>hello</strong>') )

See Fiddle Demo

Answer №2

Update the content of the body element by wrapping all instances of "hello" in strong tags

 $('body').html($('body').html().replace(/hello/g, '<strong>hello</strong>') )

Answer №3

$(document).ready(function() {
     $('*:contains("hello")').css( "text-decoration", "underline");
});

Kindly examine this code snippet

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you explain the significance of this %s value?

While going through some code, I found this: form method='post' input type='hidden' name='input' value='%s' I'm puzzled about the meaning of %s. I attempted to search online for an answer but couldn't fin ...

Effortlessly adjust the top margin of the div element

I have been attempting to smoothly move a div (circle), but I'm facing difficulties. The div instantly jumps to the final position instead of moving smoothly. In an effort to simulate the process of a ball falling, I tried using the animate method wi ...

Vertically arrange the table for better visibility

I need help with changing the table layout from horizontal to vertical. Currently, all fields are displayed horizontally and I want them to be displayed vertically. Also, I'm trying to add a functionality where the current date is added to the databas ...

Breaking apart a pipe-separated text and sending it through a JavaScript function

CSS: <div class="pageEdit" value="Update|1234567|CLOTHES=5678~-9876543?|5678"> <a href="https://host:controller">Update</a> </div> Trying to retrieve the data within the div and pass it into a JavaScr ...

An easy CSS conundrum when hovering

Looking for some assistance with CSS. I want to display the text "Featured Page" with a picture appearing on the right side upon hovering (mouseover). Currently, the picture shows up under the text using the following CSS, but I need it to be larger and pl ...

Transferring a PHP object between two PHP scripts

Is there a way to transfer a PHP object to another PHP page on a different machine? For example, I have created a class that builds a Trie. After the Trie is constructed, I want to send the object to another PHP page so it can also access the object. Wou ...

The connected callback does not trigger when custom HTML elements are being created

Attempting to craft a custom HTML tag using JavaScript, I aimed to create the custom element with ES6 JavaScript syntax. The code devised for this task reads as follows: customElements.define('neo-element', NeoElement); function NeoElement (){ ...

Center an image vertically in an AdminLte content-wrapper division

I am currently utilizing an AdminLte template as the framework for my design. In the designated area where I need to insert the main content, it is structured in the following manner: <div class="content-wrapper"> <se ...

Checking the validity of data before submission can help ensure the accuracy and security of your application

Is there a more effective method for validating input data in real-time as it is being entered into a form? One approach I am considering involves placing a DIV element next to the input field where users can see a message indicating whether their data is ...

Determine the current position of the cursor within a contenteditable division

I came across this code snippet on a forum to determine the cursor position in a contenteditable div, but unfortunately it consistently returns 0. Here is the function responsible for retrieving the cursor position: new function($) { $.fn.getCursorPo ...

Interactive horizontal slideshow for hyperlinks - bootstrap framework and model-view-controller architecture

I am currently working on an MVC project that utilizes Bootstrap. My requirement is to have a horizontal menu of links that can slide left and right using arrows (similar to a carousel, but for links instead of images). To be more specific, the menu need ...

Discrepancies in button padding across desktop and mobile platforms

I have created a sample button and included a jsfiddle link to showcase the code. Due to the font properties of the specific font I'm using, I had to adjust the padding values differently for the top and bottom of the text to achieve a vertically cent ...

Extracting a value from one HTML element and transferring it to another HTML element's attribute

Is there a way to retrieve the value of an "alt" attribute from an HTML <img> element and then assign it as the value for the "title" attribute in an enclosing <a> tag using PHP? <a title="" href ="page.html"><img src="image.jpg" al ...

Alternate. (individually)

Issue with Running Program I've been struggling to get my program to run correctly. The task at hand is to have the program display only one question at a time. But no matter what I try, clicking on all questions displays all the answers simultaneous ...

Discover updates within a JQuery Ajax call

I am sorry if this question sounds simple, but I would like to know how to set up a function that triggers when the Ajax data changes from the previous request. window.setInterval(function(){ $.get("feed", function(data){ if (data.changed ...

reduce opacity of specified div background

Objective / Purpose I am working with multiple div elements and I want to assign two distinct classes to each of them, each serving a different purpose within my application. One class should determine the level, influencing the color of the div. The oth ...

Having trouble retrieving the content within an HTML tag using jQuery?

I'm facing some challenges when it comes to extracting the content of an html tag using the Chrome console. I've been trying different methods for about half an hour, but nothing seems to work. That's why I need some help :) The code I want ...

Swap out the div with a different one when the data-rel values are sequential

I've created a puzzle and I'd like it to fade out while the entire image fades in upon correct completion. Each puzzle piece in the HTML has a data-rel attribute with numbers assigned to them, but I'm unsure how to determine if the puzzle is ...

Where can I locate information on using the .get method?

Recently, I encountered a code snippet on this site that helped me resolve an issue. The individual who provided the code utilized a .GET method that was unfamiliar to me. Here's a sample snippet: If you'd like to see the complete code, you can ...

What is the best way to ensure that the content on my webpage stays below the navbar, no matter the height of the

I've been working on my homepage layout and encountered an issue with positioning the content below the navbar, which is centered on the page. Every time I adjust the window height, the content shifts. How can I ensure that it stays fixed below the na ...