2

I have one HTML table and I don't want the value of second column to repeat in the grid. Here is my JavaScript:

var $addedProductCodes = [];

function getProductData(value){

        $td_productCode=$("#sales-product-code").val();
        var index = $.inArray($td_productCode, $addedProductCodes);
        if (index >= 0) {
                alert("You already added this Product");
            } else {
                $('#test').append("<tr><td>"+ value +"</td></tr>");
                $addedProductCodes.push($td_productCode);
            }
        }

and Html

    <tbody  id="test">
  <tr>
    <td><input type="text" id="sales-product-code" name="cm_code[]" value="" class="sales-product-code"/> </td>
  </tr>
</tbody>

Please help me out. Help is highly appreciated.

2 Answers 2

2

The call to

$('#test').append("<tr><td>"+ value +"</td></tr>") 

should be replaced with

$('#test tr:last').after("<tr><td>" + value + "</td></tr>").

I created a JSFiddle example.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use a boolean variable and use it in your conditional if. If the column is added, change the value for that variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.