Translate

Thursday, 3 December 2015

How to check Duplicate row values in Html table using jquery


 Check duplicete values in html table using Jquery in below ways

First Way:

    <script src="Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {

            function IsDuplicate(btnId, tableId) {
                if ($(btnId).length > 0 && $(tableId).length > 0) {
                    $(btnId).click(function () {
                        alert($('#' + $(txtvalue).val() + '').length);
                        if ($(tableId).find('#' + $(txtvalue).val() + '').length > 0) {
                            alert('d');
                        } else {
                            alert('s');
                        }
                    });
                }
                else { return false; }
            }

            IsDuplicate('#btnDuplicate', 'table');
        })

    </script>
Second Way:


<script>
        $(document).ready(function () {
            $("#btnDuplicate").click(function () {
                var count = 0;
                $('#tblcheck tr').each(function () {
                
                    var customerId = $(this).find("td").eq(1).html();
                    if (customerId == $("#txtvalue").val()) {
                       count++
                    }
                  
                });
                if (count > 0) {
                    alert("Duplicate");
                }
                else {
                    alert("No Duplicates");
                }
            });
        });

    </script>


Html Table:

</head>
<body>
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <tr id="1">
            <td>1</td>
            <td>Kavitha</td>
        </tr>
        <tr id="2">
            <td>2</td>
            <td>Hello</td>
        </tr>
        <tr id="3">
            <td>3</td>
            <td>Hello1</td>
        </tr>
    </table>
    <input type="text" id="txtvalue" />
    <input type="button" id="btnDuplicate" value="Duplicate" />
</body>

</html>