Vandan29
441
13
Laravel

How to pass Input field id pass in script

Posted 1 month ago by Vandan29

IN Laravel same blade file use two script one script was multiple Qty and Amount Second script was add dynamic input field First script work on just first field after I add dynamic field script doesn't work

<script>
        var qty = '#qty';
        var amount = '#amount';
        var theTotal = 0;
        $('input').change(function(){
        theTotal = Number($(qty).val()) * Number($(amount).val());
            $('#tussenstand').val("Total: "+theTotal);        
        });

        var total = $('#tussenstand').val("Total: "+theTotal);
</script>

<script type="text/javascript">
        $(document).ready(function(){
        var postURL = "<?php echo url('addmore'); ?>";
        var i=1;

        $('#add').click(function(){
        i++;
        $('#dynamic_field').append('<tr id="row'+i+'" class="dynamic-added"><td></td><td><input                                                                 type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td><input type="number" name="qty[]" id="qty" class="form-control name_list" /></td><td><input type="number" name="amount[]" id="amount" placeholder="Enter your Amount" class="form-control name_list" /></td><td><input type="text" name="subtotal[]" id="'+total+'" class="form-control" disabled></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
        });

        $(document).on('click', '.btn_remove', function(){
        var button_id = $(this).attr("id");
        $('#row'+button_id+'').remove();
        });

        $.ajaxSetup({
        headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#submit').click(function(){
        $.ajax({
                url:postURL,
                method:"POST",
                data:$('#add_name').serialize(),
                type:'json',
                success:function(data)
                {
                        if(data.error){
                            printErrorMsg(data.error);
                        }else{
                            i=1;
                            $('.dynamic-added').remove();
                            $('#add_name')[0].reset();
                            $(".print-success-msg").find("ul").html('');
                            $(".print-success-msg").css('display','block');
                            $(".print-error-msg").css('display','none');
                            $(".print-success-msg").find("ul").append('<li>Record Inserted Successfully.</li>');
                        }
                }
        });
        });
</script>

html in

Add More

Please sign in or create an account to participate in this conversation.