0

I am trying to use the JQuery click event to remove a class and add a different class but my SVG object is not changing the fill as expected.

 $(".seat").click(function (event) {
      var result = $(this).data("seat");
      var state = $(this).data("state");
      $(this).removeClass( "seat" ).addClass( "taken" );
       result = result +" - "+ state;
       $("#statusBox").html(result);

       });

My JS fiddle is Here

John S
  • 7,909
  • 21
  • 77
  • 145
  • Your answer can be found here: http://stackoverflow.com/questions/8638621/jquery-svg-why-cant-i-addclass – John Apr 21 '14 at 22:01

2 Answers2

2

Unfortunately, class does not work the same for SVG tags as it does for standard HTML, at least in respect to jQuery. Rather than treat it as you would a class, you need to treat it like an attribute.

$(".seat").on('click',function(e){
    var $this = $(this),
        $data = $this.data();

    $this.attr('class','taken');

    $("#statusBox").html($data.seat +" - "+ $data.state);
});

Since you were removing the only class present and giving it a new class, I simply assigned the attribute of class the new value of taken. If your actual code is more complex with multiple classnames, you may need to get creative, but the concept remains the same ... replacing that part of the attribute's string value.

I also updated your code to be a bit more efficient, hope thats cool. Here is the working jsFiddle.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
0

I don't think addClass() or removeClass() work with svg. If I remember correctly you'll need to use .attr()

Demo Fiddle

JS:

$(".seat").click(function (event) {
       var result = $(this).data("seat");
       var state = $(this).data("state");
       $(this).attr('class', 'taken');
       result = result +" - "+ state;
       $("#statusBox").html(result);                     
});
badAdviceGuy
  • 2,060
  • 1
  • 16
  • 12