0

How can I prevent duplicates when clicking a seat so it doesn't append when I click it again, instead it is removed/added consecutively.

  var ids = [];
  $seatid = ids;
  var status = "unavailable";

  $(document).ready(function() {
    var $div = $("<div id='form'></div>").appendTo(".appended"), code;

      $('.seat').click(function(){
        var id = jQuery(this).attr("id");

        if ($(this).hasClass('available')){
          code = jQuery(this).attr("id");
          ids.push(code);
          $div.append(code + "<br />");
          console.log(code);

        }else{
          console.log('Failed');
          console.log(id);
        }                        
      });
  });

see pic of the output

1
  • You haven't given us enough information to help you. Please see why and how to create minimal reproducible example, emphasis on minimal. Use <> button in editor to create a snippet. Commented Apr 25, 2021 at 14:57

2 Answers 2

1

You'll need to remove the class "available" after you push the new ID. I'm not well versed in JQuery, but it would look something like this:

if ($(this).hasClass('available')){
      code = jQuery(this).attr("id");
      ids.push(code);
      $div.append(code + "<br />");
      console.log(code);
      //REMOVE AVAILABLE CLASS
      $(this).removeClass("available");
    }

You'll probably need to re-add the class when you run your else statement (assuming you want to make an "unavailable" seat become "available" when you click it).

You will also need some logic to remove the seatID from your array when you go from "unavailable" (in the array) to "available" (not in the array)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use toggleClass() function of Jquery in order to do that.

for example, if u want to remove or add class "available" and "selected", the following code would work well.

$('#x').click(function(){
  $('#x').toggleClass('available').toggleClass('selected');
})

Also, if you want to remove item from an array. you can use the following code.

const array = [12, 34, 10];

const index = array.indexOf(12);
if (index > -1) {
  array.splice(index, 1);
}

// array = [34, 10]
console.log(array);

At last, your code should be updated as following..

var ids = [];
$seatid = ids;
var status = "unavailable";

$(document).ready(function() {
var $div = $("<div id='form'></div>").appendTo(".appended"), code;

  $('.seat').click(function(){
    var id = jQuery(this).attr("id");
    if ($(this).hasClass('available')){
        code = jQuery(this).attr("id");
        ids.push(code);
        $div.append(code + "<br />");
    } else {
        const index = ids.indexOf(code);
        if (index > -1) {
          ids.splice(index, 1);
        }
        console.log('Failed');
        console.log(id);
    }                        
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.