Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9049985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:32:30+00:00 2026-06-16T12:32:30+00:00

I am trying to change first color of define class or tag by using

  • 0

I am trying to change first color of define class or tag by using jquery. Things work fine but I have one title having own span and giving trouble. I am using below code to add span element and inline style for first word.

$(document).ready(function()
{

    $('.header-logo, h1').each(function() {
        var h = $(this).html();
        var index = h.indexOf(' ');
        if(index == -1) {
            index = h.length;
        }
        $(this).html('<span style="color:black;">' + h.substring(0, index) + '</span>' + h.substring(index, h.length));
    });

});

This is adding span tag perfectly and change the color too. But I have one title with below html structure

<h1>
    <span id="fav" class="btn-fav"><a href="#"Add to fav</a></span>
    <span class="the-title">Title text goes here</span>
</h1>

Here I want to add color only for “Title text goes here” first word (Title) but the javascript breaking structure by adding another span tag into the span tag something like below

<span style="color:black;">
<span< span=""> class="btn-fav id="fav">.....

So how can I resolve this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:32:31+00:00Added an answer on June 16, 2026 at 12:32 pm

    You can use html method, html accepts a function, you can find the first word and wrap it by span element.

    $('h1 .the-title').html(function(i, html){
       return html.replace(/(\w+\s)/, '<span class="black">$1</span>')
    })​;
    

    http://jsfiddle.net/AuKWT/

    Note that you should fix your markup:

    <span id="fav" class="btn-fav"><a href="#"Add to fav</a></span>
                                       ------^
    

    $('.header-logo, h1').each(function() {
        var $this = $(this), $title = $this.find('.the-title');
        if ($title.length) {
            $title.html(function(i,h) {
                return h.replace(/(\w+\s)/, '<span class="black">$1</span>')
            })
        } else {
            $this.html(function(i,h) {
                return h.replace(/(\w+\s)/, '<span class="black">$1</span>')
            })
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JQuery's jquery.animate-colors-min.js plugin I am trying to change the background color of some
I am actually trying to change the color index for the first word with
I'm trying to change the color of the first item in this menu (that
I'm trying to make the background color change on :hover but keep the background
i trying to change background color on mouse over. i have this css code
I am using DataGrid and trying to change the default background color for every
I have the following jQuery that I'm trying to use to change the background
I am trying to change the color of rows in datatable using the below
I'm trying to change the background color by adding a new class with another
I'm learning css and trying to change background-color of all html except one div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.