Sachin Agarwal
Sachin Agarwal

Reputation: 5

Remove an ID if a input value is = some defined code

I want to remove an input id when another input id having value in defined values

my codes

setInterval(function() {
  var MaterialGod = [
    "FPPUTHP1100000",
    "FPPUTHP1100000",
    "FPPUTHP1110000",
    "FPPUTHP1500000",
    "FPPUTHP1680000",
    "FPPUTHP1690000",
    "FPPUTHP1590000"
  ];

  $.each(MaterialGod, function(index) {
    if ($("label:contains('Meterial Code')").parent().next().find('input').val() == index) {
      $('#__item5-__box2-0').remove();
    }
  })
}, 100);

Pls give your advise what i m doing wrong

Upvotes: 0

Views: 99

Answers (2)

Satinder singh
Satinder singh

Reputation: 10208

If am not wrong you are comparing with index instead of value. Try comparing with value as shown in below code.

  setInterval(function() {
      var MaterialGod = [
        "FPPUTHP1100000",
        "FPPUTHP1100000",
        "FPPUTHP1110000",
        "FPPUTHP1500000",
        "FPPUTHP1680000",
        "FPPUTHP1690000",
        "FPPUTHP1590000"
      ];
    
      $.each(MaterialGod, function(index,iVal) {
        if ($("label:contains('Meterial Code')").parent().next().find('input').val() == iVal) {
          $('#__item5-__box2-0').remove();
        }
      })
    }, 100);

Upvotes: 2

Barmar
Barmar

Reputation: 782148

You're comparing the array index, not the array element, with the value.

There's also no reason to put the selector in the loop, since you're selecting the same input every time.

Just get the input value, and use .includes() to see if it's in the array.

setInterval(function() {
  var MaterialGod = [
    "FPPUTHP1100000",
    "FPPUTHP1100000",
    "FPPUTHP1110000",
    "FPPUTHP1500000",
    "FPPUTHP1680000",
    "FPPUTHP1690000",
    "FPPUTHP1590000"
  ];
  if MaterialGod.includes($("label:contains('Meterial Code')").parent().next().find('input').val()) {
    $('#__item5-__box2-0').remove();
  }
}, 100);

Upvotes: 1

Related Questions