Underline specific text within a <p> tag using JavaScript
To bold and underline specific text within a <p>
tag using JavaScript, you can wrap those specific words in <span>
tags with inline styles or classes for styling. Here's an example:
HTML Structure
JavaScript
Explanation
- Identify the Phrases: The
phrasesToStyle
array contains the phrases to be bolded and underlined. - Retrieve the Content: The original text of the
<p>
tag is retrieved usinginnerHTML
. - Replace Each Phrase: Use
replace()
to wrap each phrase with a<span>
tag and apply inline styles for bold and underline. - Update the Content: Set the updated
innerHTML
back to the<p>
tag.
Output
The specified words in the paragraph will appear bold and underlined, like this: Love and Marriage, Finances, Cautions, etc. The text-decoration property is used to specify whether the text should be Underline, overline, line-through, or blink. Underline and overline decorations are positioned under the text, while line-through is placed above it.
Post a Comment
0Comments