0

My table Looks like this:

<table style="width:100%" class="table table-striped table-bordered" id="xxxxxx">
    <thead>
        <tr>
            <th>Item Id</th>
            <th>Item Name</th>
            <th>Rate</th>
            <th>Quantity</th>
            <th>Total</th>
            <th>Edit</th>
        </tr> 
    </thead>
</table>

I am adding data to my table on a button click like this:

var item_x = $('#itemIdx').val();
var item_x_name = $('#itemx option:selected').val();
var rate_x = $('#ratex').val();
var quantity_x = $('#quantityx').val();
var total_x = rate_x * quantity_x;

var add_to_table = '<tr><td>' + item_x + '</td><td>' + item_x_name + '</td><td>' + rate_x + '</td><td>' + quantity_x + '</td><td>' + total_x + "</td><td><buttonDelete</button></td>";
$('#xxxxxx').append(add_to_table);

Now While adding data dynamically I want to duplicate data based on item_x value whether it is repeated or not. If repeated then an alert is to be shown; if not simply add to the xxxxxx to table.

1 Answer 1

2

You can check the existence using:

var tdlength= $("td").filter(function() {
    return $(this).text().toLowerCase() == item_x ;//get td with item_x 
 }).length;

if(!tdlength){ //td with html item_x  does not exists.
   //code here
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You Milind..but i dont want any duplicates data to be added..item_x is getting populated on the change event of "item_x_name"..so if i find 2 same item_x i wont allow it to add in the table..on a button click..
You can put that if condition in click event.

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.