Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

edit css to event.target

I am trying to make background change CSS attribute to event.target and it is not working. All the commands are working except changing event.target.css

Here is the code I'm using:

$(document).ready(function() {
    $(".plusMatch").click(function(event) {

        if ($("#productImage"+event.target.id).hasClass("visible")) {
            $("#productImage"+event.target.id).css("display", "none");
            $("#productImage"+event.target.id).removeClass('visible');
            event.target.css("background", "url(minusSign.jpg)");

        } else {
            $("#productImage"+event.target.id).css("display", "block");
            $("#productImage"+event.target.id).addClass('visible');
            event.target.css("background", "url(minusSign.jpg)");
        }


    });
});
like image 984
bbtron Avatar asked Sep 02 '16 17:09

bbtron


1 Answers

The first thing is that you are using JQuery in your project, so it is better for you to write $(event.target) instead of event.target if you want to use the JQuery CSS property.

Then, to change any CSS property with javascript, you have two options :

The native style property will edit the style tag on your HTML element, for example :

event.target.style.background = 'url(myPicture.jpg)';

More informations

The JQuery css property

$(event.target).css('background': 'url(myPicture.jpg)');

More informations

Both of these solutions will produce the following result :

<div style="background:url('myPicture.jpg');"></div> 
like image 68
Dev Avatar answered Oct 18 '22 14:10

Dev