yvonnezoe yvonnezoe - 19 days ago 16
jQuery Question

jQuery How to check if an entry has already exist in a table?

i'm using jquery to dynamically adding and removing rows of a table. Something like this.

My question is, how can i check if the entry (BOTH

output type
AND
output number
) has already exist in the table? so that i won't add 2 or more similar entries but update the existing one instead or just ignore or gave an alert...

I'm clueless about the checking part. Do I need a database??

if (textInput== ??existing entry??)
alert ("you have entered that output number for that output type");
// quit the codes below or something?

Answer
   function isExist(newEntry){
     return Array.from($('tr[id*=output_newrow]'))
              .some(element => $('td:nth(2)', $(element)).html() === newEntry );
    }

newEntry is the value of input text to add Then :

$('.add').click(function () {

    textInput = "";
    $('.TextInput').empty();
    textInput =  $(this).prev('.TextInput').val();

    if(isExist(textInput)){
     alert("you have entered that output number for that output type")
    }else{

         //.....insert code
    }
})

Demo :

http://jsfiddle.net/abdennour/MKfLU/27/

"but it applies to different selection option but same input number as well... can i do isExist (textInput)AND(type)? " If you want to embed type in the test :

function isExistx(newEntry,outtype){

  return Array.from($('tr[id*=output_newrow]')).some( el => 
    ( $('td:nth(1)',$(el)).html() === outtype ) && ($('td:nth(2)',$(el)).html() === newEntry)
  ); 

}

Then :

  if(isExistx(textInput,type)){
        alert('you have entered that output number for that output type')
    }else {

         $('#status_table tr:last').after(str);
    }

Demo

http://jsfiddle.net/abdennour/MKfLU/29/