A couple of questions about jQuery addClass()

Posted on 2015-01-16
Last Modified: 2015-01-16
I have two questions:

1) I have a <div> with child elements. When I do an addClass(".yellowBackground") to the <div>, all its children are getting it too. How can I prevent that from happening so that only the <div> gets the class?

2) Also, it seems like there is duplication happening at times where an element would have the same class added in twice. I'm not sure why that's happening because when I tested addClass(), adding an already existent class does not duplicate it. Any idea what might be causing this?

Question by:elepil
  • 2

Accepted Solution

Valliappan AN earned 400 total points
ID: 40553984
1) $( "div:first" ).addClass("yellowBackground') should work, please try and let know.

2) In case you add styles from multiple addClass, multiple matching selectors at various places, or an inline style or a page or external style may be collapsing with your addClass. Try to find the roots may be using Firebug in Firefox, or Inspect Element in Chrome, or IE.


Assisted Solution

by:Jim Riddles
Jim Riddles earned 100 total points
ID: 40554173

Do you have some sample code (HTML and Javascript) that you can show?  That would be a huge help in solving your issue.

Author Comment

ID: 40554577
Okay, I think I know why my code isn't working. Let me present the code first, and I'll explain:

<!DOCTYPE html>
        <meta charset="UTF-8">
        <script src="//"></script>
        <script type="text/javascript">
            $(function() {
                $(".event").hover(fnMouseIn, fnMouseOut);
                function fnMouseIn(event) {
                function fnMouseOut(event) {
            div {
                height: 100px;
            .yellowBackground {
                background-color: cornsilk;
            .highlightEventDate {
                font-weight: bold;
                text-decoration: underline;
                color: red;
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>

Open in new window

My objective is simple -- when the mouse hovers over the <div>, the background changes to yellow, and the <li class="eventDate"> become bold, underlined, and red.

By the way, I DO know there are better ways of doing this, but in reaching my ultimate solution, I stumbled across this scenario, and I just wanted to understand why it wasn't working right, and I think I know why.

I think the problem is that even though I created the hover event for the <div>, even the child elements are propagating the event upwards, causing to change, causing my code to apply the style to the wrong element.

So I guess my real problem is, how can I make the mouseover and mouseout events NOT fire with the child elements but only the <div>?

Author Closing Comment

ID: 40554797
I actually found the answer. I gave Valliappan AN more points because his post was actually a recommended solution. I gave Jim Riddles some points for simply responding.

Thank you both!

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 48
jQuery UI required not working in safari 9 34
In Angular, how can I programatically fire a function 8 64
Jquery keyup 4 17
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question