• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

A couple of questions about jQuery addClass()

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?

Thanks.
0
elepil
Asked:
elepil
  • 2
2 Solutions
 
Valliappan ANSenior Tech ConsultantCommented:
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.

HTH.
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
Elipil,

Do you have some sample code (HTML and Javascript) that you can show?  That would be a huge help in solving your issue.
0
 
elepilAuthor Commented:
Okay, I think I know why my code isn't working. Let me present the code first, and I'll explain:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <meta charset="UTF-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $(".event").hover(fnMouseIn, fnMouseOut);
                
                function fnMouseIn(event) {
                    console.log($(event.target).prop("tagName"));
                    $(event.target).addClass("yellowBackground");
                }
                function fnMouseOut(event) {
                    console.log($(event.target).prop("tagName"));
                    $(event.target).removeClass("yellowBackground");
                }
            });
        </script>
        <style>
            div {
                height: 100px;
            }
            .yellowBackground {
                background-color: cornsilk;
            }
            .highlightEventDate {
                font-weight: bold;
                text-decoration: underline;
                color: red;
            }
        </style>
    </head>
    <body>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        </div>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        </div>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        </div>
        <div class="event">
            <ul><li class="eventDate">January 7, 2015</li></ul>
        </div>
    </body>
</html>

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 event.target 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>?
0
 
elepilAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now