jquery addClass() not working with event.target
Asked Answered
D

7

22

Please help.

Why is the jquery addClass() not working with event.target? I have made a code and it supposed to add class on the target when clicked, but it does not work, and it says,e.target.addClass is not a function.

http://jsfiddle.net/Lq9G4/

CODE:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Class Test</title>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <style>
        .big {
            font-size: 5em;
        }
        .small {
            font-size: 1em;
        }
    </style>

</head>
<body>
    <p>This is the text</p>
    <script>
        $(function() {
            $("p").click(function(e) {  
                      $("a").removeClass("big").addClass("small");
                      $("this").addClass("big"); //This doesn't work
                      e.target.addClass("big"); //nor this one                
                    });
        });

    </script>
</body>
</html>
Daria answered 22/4, 2014 at 5:32 Comment(0)
J
46

Basically e.target will be a javascript object, you have to convert it into a Jquery object before utilizing its functions like .addClass()

Try,

$(e.target).addClass("big"); 

and at the same time, $("this").addClass("big"); this code will not work since you are passing this as a string. this also a javascript object, you need to convert that too as a jquery object like $(this)

Jassy answered 22/4, 2014 at 5:32 Comment(1)
@user3540736 i had given some explanations regarding that in my answer.. :)Jassy
V
3

You have two problem:

$(this).addClass("big"); //Unquote to "this"
$(e.target).addClass("big"); // select e.target with $() wrapper.
Vaccine answered 22/4, 2014 at 5:34 Comment(0)
D
2

Change this line

$("this").addClass("big"); //This doesn't work

to

$(this).addClass("big"); //This will work work

And further if you need it with the event itself then you can use

$(e.target).addClass('big');
Drury answered 22/4, 2014 at 5:33 Comment(0)
B
2

Since .addClass() is a jQuery method, you need a jQuery object, so convert e.target to jQuery object by wrapping it inside $:

$(e.target).addClass("big"); 

Beside that, the other solution is to use $(this). you don't need to wrap this inside " ":

$(this).addClass("big");

Updated Fiddle

Brokenhearted answered 22/4, 2014 at 5:33 Comment(0)
G
2

Try this:

 $(e.target).addClass('big');

Demo

Gaither answered 22/4, 2014 at 5:33 Comment(0)
H
2
<script>
    $(function() {
        $("p").click(function(e) {
                  $("a").removeClass("big").addClass("small");
                  $(this).addClass("big"); //This doesn't work               
                });
    });

</script>

"this"=>this

Halflight answered 22/4, 2014 at 5:35 Comment(0)
W
2
    e.currentTarget.classList.add("loading_arrow");

you can use above line to add loading_arrow class using js mouse event.

Whitesmith answered 16/5, 2019 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.