$('.box').click(function(){
$(this).find('p').toggle(
function () {
$(this).css({"color" : "red"});
},
function () {
$(this).css({"color" : "black"});
}
);
});
if i execute this script the ‘p’ color will change if i click on the ‘p’ not on the .box class why ? And how to i make it so that ‘p’ color changes when i click on the .box div
.toggle()assigns event handlers to theclickevent, what you want is this:Every time you wrap a new function,
thisrefers to the element you’re dealing with (at least, for this example, see more about how closures work here), so at first it’s the.boxselector match, then it’s eachpinside, you want to assign a click toggle on.boxto switchp‘s color, so you use a.toggle()on.boxdirectly.