-1

I want to remove a specific value from an array.
For example:

var categories = ["Lenovo","Large","100"]; 

I displayed it like this

Website screenshot

HTML CODE:

<div style="margin-bottom:5px;">
  <select class="form-control product-type" id="brand" >
    <option value="">Select a Brand</option>
    <option value="Lenovo">Lenovo</option>
    <option value="Acer">Acer</option>
  </select>
</div>

<div style="margin-bottom:5px;">
  <select class="form-control product-type" id="screen_size" style="margin-top:0px;">
    <option value="">Select a Screen Size</option>
    <option value="Small">Small</option>
    <option value="Large">Large</option>
  </select>
</div>

<div style="margin-bottom:5px;">
  <select class="form-control product-type" id="cpu">
    <option value="">Select a CPU</option>
    <option value="Intel">Intel</option>
    <option value="Amd">Amd</option>
  </select>
</div>

<div style="margin-bottom:5px;">
  <select class="form-control product-type" id="memory">
    <option value="">Select a Memory</option>
    <option value="500mb">500mb</option>
    <option value="1tb">1tb</option>
  </select>
</div>
<div style="margin-bottom:5px;">
  <select class="form-control product-type" id="price">
    <option value="">Filter by Price</option>
    <option value="10000">10000</option>
    <option value="20000">20000</option>
  </select>
</div>

How can I achieve this? I tried it so many times but I failed because I cant get the value. You can check my code:

jQuery("#filter").val()
jQuery(function() {
  jQuery("#brand,#screen_size,#cpu,#memory,#price").change(function() {

    var brand = jQuery("#brand").val();
    var screen_size = jQuery("#screen_size").val();
    var cpu = jQuery("#cpu").val();
    var memory = jQuery("#memory").val();
    var price = jQuery("#price").val();
    var categories = [];
    if (brand) {
      categories.push(brand);
    }
    if (screen_size) {
      categories.push(screen_size);
    }
    if (cpu) {
      categories.push(cpu);
    }
    if (memory) {
      categories.push(memory);
    }
    if (price) {
      categories.push(price);
    }
    length = categories.length;
    categories2 = categories.toString();

    var categories3 = "";
    for (var i = 0; i < length; i++) {
      var cat_id = "cat" + i;
      categories3 += "<div class='filter_style'>" + categories[i] + "<a href='" + cat_id + "'  id='" + cat_id + "' onclick='removeCat(event)'><span style='margin-left:15px;color:gray;' >x</span></a></div>";
      jQuery("#filter").html(categories3);

    }   
  });
});

function removeCat(evt) {
  evt.preventDefault();
  var catid = jQuery(this).attr('href');
  var cat = jQuery(this).data("id");
  //jQuery.grep(); maybe or indexOf first then slice() but I do not get the value
  alert(catid);
}
Kyll
  • 6,830
  • 6
  • 39
  • 56
Jonas Dulay
  • 273
  • 2
  • 17
  • Please post your html code as well for better understanding – Umair Farooq Aug 16 '16 at 06:40
  • 1
    Possible duplicate of [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/how-to-remove-a-particular-element-from-an-array-in-javascript) – aldrin27 Aug 16 '16 at 06:43

2 Answers2

1

You can simply remove element from array using below method

categories .splice( $.inArray(removeItem, categories), 1 );//removeItem is name of item which you want to remove from array
Jekin Kalariya
  • 3,255
  • 2
  • 18
  • 31
0

If you want to remove, for example, "Lenovo" from:

 var categories = ["Lenovo","Large","100"]; 

do:

 categories.splice(0)