Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu February 21, 2024 . #HowTo . #Javascript

How to Make the First Word of Each Statement Bold Using Javascript

To make the first word of each statement bold using JavaScript, you can manipulate the text content of HTML elements. Here's a simple example of how to do this:

<p class="statement">This is a paragraph.</p>
<p class="statement">This is a paragraph 2.</p>
<script>
  // Select all elements containing statements
  const statements = document.querySelectorAll('.statement');

 // Loop through each statement element
  statements.forEach(statement => {
    // Get the text content of the statement
    const text = statement.textContent.trim();

    // Split the text into words
    const words = text.split(' ');

    // Wrap the first word in a <strong> tag
    words[0] = `<strong>${words[0]}</strong>`;

    // Join the words back together
    const newText = words.join(' ');

    // Update the text content of the statement element
    statement.innerHTML = newText;
 });
</script>

In this example, .statement is the class name of the elements containing the statements. Adjust the selector according to your HTML structure.

This script will find each element with the class .statement, split its text content into words, wrap the first word in a <strong> tag, and then update the HTML content accordingly.

save
listen
AI Answer
Write Your Answer
loading
back
View All