-1

The HTML Code

echo  "<ul id='sub'>";
if($Set == null){ 
echo '<li class="BlankSeat" ></li>';
}

if in database status yes or no add class in red color

elseif($status == 'no' || $status == 'yes'){
echo '<li class="occupied" title="Row'.$val1.'" name="'.$val2.'" 
value="'.$val3.'"></li>'; 
 }
 elseif($name=='PINK'){
echo '<li class="pink" title="Row'.$val1.'" name="'.val2.'" 
value="'.$val3.'"></li>';
}
elseif($name=='YELLOW'){
echo '<li class="yellow" title="Row'.$val1.'" name="'.$val2.'" 
value="'.$val3.'"></li>';
 }
else{
 echo '<li class="orange" title="Row'.$val1.'" name="'.$val2.'" 
 value="'.$val3.'"></li>';
 }
echo "</ul>";   
}
echo '</div>';
}
else
{
 echo "No result Available";
 }

This is my code

$('li').click(function(e) {
var $this = $(this);

First part where iam adding class when click on one of them

if($(this).hasClass('pink')|| $(this).hasClass('yellow') || 
$(this).hasClass('orange'))
{
$(this).addClass('Booked').removeClass('pink','yellow','orange');
}

Here i want to remove the booked class with only which one is clicked

else
{
$(this).addClass('pink').removeClass('Booked');
$(this).addClass('yellow').removeClass('Booked');
$(this).addClass('orange').removeClass('Booked');
}}
krish
  • 120
  • 14

2 Answers2

1

Try this:

$(document).on("click", "li", function(e) {
   if($(this).hasClass('pink')|| $(this).hasClass('yellow') || $(this).hasClass('orange')){
      $(this).toggleClass("Booked")
      console.log($(this).attr("class"))
   }
})
.pink{
  background-color:pink;
}
.yellow{
  background-color:yellow;
}
.orange{
  background-color:orange;
}
.Booked{
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id='sub'>
  <li class="BlankSeat"> Item 3</li>
  <li class="occupied"> Item 3</li>
  <li class="pink"> Item 3</li>
  <li class="yellow"> Item 4</li>
  <li class="orange"> Item 5</li>
</ul>
SilverSurfer
  • 4,281
  • 6
  • 24
  • 50
0

Use this

$(this).addClass('Booked').removeClass('pink yellow orange');

See help here

Edit:

Sample JS to my comment below

if($(this).hasClass('pink') || 
   $(this).hasClass('yellow') || 
   $(this).hasClass('orange')) {
    $(this).addClass('Booked');
} else if($(this).hasClass('Booked')) {
    $(this).removeClass('Booked');
}

Sample CSS to my comment below

.Booked {
    background-color: blue !important; // whatever style your .Booked should have
}
Raphael
  • 990
  • 1
  • 13
  • 24
  • 1
    first part is okay.but in else part when Booked class is active ,now when i again click on Booked ,after that the same class should be added which last time changed – krish Sep 12 '17 at 06:15
  • Well if you want to restore the previous class you can go with this: Add Booked class on click, but do not remove pink yellow orange. Now .Booked should style the
  • with !important styles
  • – Raphael Sep 12 '17 at 06:25
  • Added a sample above – Raphael Sep 12 '17 at 07:08