yvonnezoe yvonnezoe - 1 year ago 105
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
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 Source
   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 =  $(this).prev('.TextInput').val();

     alert("you have entered that output number for that output type")

         //.....insert code

Demo :


"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 :

        alert('you have entered that output number for that output type')
    }else {

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



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download