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

  • SEARCH
  • Home
  • 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 6252305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:46:22+00:00 2026-05-24T13:46:22+00:00

I have this code,everything works fine but .css(‘color’,’red’) rule is not getting applied.I tried

  • 0

I have this code,everything works fine but .css('color','red') rule is not getting applied.I tried different ways but could’nt find any.

$('#menu li').each(function() {
$(this).hover(function() {
$(this).text().charAt(0).css('color','red');
});
});

But this code works fine.Cant figure what is wrong with above code.

$('#menu li').each(function() {
$(this).hover(function() {
$(this).css('color','red');
});
});

Thank you for your responses.

  • 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-05-24T13:46:23+00:00Added an answer on May 24, 2026 at 1:46 pm

    $(this).text().charAt(0) is a javascript string. It has no method .css().

    So, you can’t apply CSS to a single character in a javascript string.

    If you want to apply a color to one character in the page, you’ll have to get the text, add a span tag around the first character, add style to that, then set that HTML back on the DOM element.

    I’d suggest changing your code to this:

    $('#menu li').hover(function() {
        var text = $(this).text();
        text = "<span style='color: red'>" + text.charAt(0) + "</span>" + text.slice(1);
        $(this).html(text);
      }, 
      function() {
        $(this).html($(this).text());
    });
    

    You can see that one in action here: http://jsfiddle.net/jfriend00/9xCak/

    I would not recommend using this jQuery because there are much better ways to do this. It can be done with pure CSS or you could just add/remove a class with your jQuery.

    Note, that you don’t need the each loop as you can just set a hover handler on the first jQuery selector and it will do .hover for all matched elements in the selector. Also, you have a second function in the hover call to set it back when the mouse leaves..

    If you intend for the red to go away when the mouse leaves the li, then you could do this entirely with CSS. No jQuery is required.

    HTML:

    <li><span class="firstLetter">F</span>irst Menu Item</li>
    

    CSS:

    li .firstLetter:hover {color:red;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, which works fine, but I would like to be able
I implemented this Jquery show/hide code and everything works fine except that the box
NOTE: THE PROBLEM IS NOT IN THIS CODE, BUT THE ANSWER FOR EXC_BAD_ACCESS is
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:
I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row

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.