The Wayback Machine - https://web.archive.org/web/20230330184830/https://www.geeksforgeeks.org/how-to-make-first-word-bold-of-all-elements-using-jquery/
Skip to content
Related Articles
Open in App
Not now

Related Articles

How to make first word bold of all elements using jQuery ?

Improve Article
Save Article
Like Article
  • Last Updated : 31 Dec, 2020
Improve Article
Save Article
Like Article

In this article, we will make first word bold of all elements using jquery. To make the first word bold, we use html(), and text() methods.

html() Method: The html() method in jQuery is used to set or return the innerHTML content of the selected element.

Syntax: It sets the content of matched element.

$(selector).html(content)

text() Method: This method is used to set or return the text content of the element. While setting the content, it overwrites the content of all the matched elements. The returned content of text method() is used to return the text content of all matched elements.

Syntax: It sets the text content.

$(selector).text(content)

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        How to make first word bold
        of all elements using jQuery?
    </title>
  
    <!-- Import jQuery cdn library -->
    <script src=
    </script>
  
    <script>
        $(document).ready(function () {
            $("p").each(function () {
                var txt = $(this);
                txt.html(txt.text()
                .replace(/(^w+)/, '<strong>$1</strong>'));
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to make first word bold of
        all elements using jQuery?
    </h3>
      
    <p>GFG is a computer science portal</p>
   
    <p>Computer Science Portal</p>
</body>
  
</html>

Output:


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!