Title Rewrite: Unable to react to Sum function
P粉360266095
P粉360266095 2024-02-26 18:56:31
0
1
415

I have enabled the OnClick functionality using JavaScript. But during the final process, i.e. the summation, the code I inserted didn't react.

<div class="modal fade" id="tambahgajiModal" tabindex="-1" role="dialog" aria-labelledby="tambahgajiModal" aria-hidden="true" data-backdrop="static" data -keyboard="false">
<div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="tambahfungsiModal">Tambah Gaji Karyawan</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">×</span>
            </button>
        </div>
        <div class="modal-body">
            <form action="/gaji/insert" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="content">
                    <div class="row">
                        <div class="col-sm-12">
                            <!--on clik on change javascript-->
                            <script type="text/javascript">
                                function golongan() {
                                    var data = document.getElementById("gol").value;
                                    document.getElementById("tunjangan_gol").value = data;
                                }

                                function fungsional() {
                                    var data = document.getElementById("fungsi").value;
                                    document.getElementById("tunjangan_fungsi").value = data;
                                }

                                function struktural() {
                                    var data = document.getElementById("struktur").value;
                                    document.getElementById("tunjangan_struktur").value = data;
                                }

                                function sum() {
                                    var txtFirstNumberValue = document.getElementById('tunjangan_gol').value;
                                    var txtSecondNumberValue = document.getElementById('tunjangan_fungsi').value;
                                    var txtThreeNumberValue = document.getElementById('tunjangan_struktur').value;
                                    var result = parseFloat(txtFirstNumberValue)   parseFloat(txtSecondNumberValue)   parseFloat(txtThreeNumberValue);
                                    if (!isNaN(result)) {
                                        document.getElementById('total').value = result;
                                    }
                                }
                            </script>
                            <div class="form-group">
                                <select name="nama" type="text" class="form-control @error('nama') is-invalid @enderror" value="{{ old('nama') }}">
                                    <option>-- Nama Karyawan --</option>
                                    @foreach ($karyawan as $data)
                                    <option value="{{ $data->nama }}">{{ $data->nama }}</option>
                                    @endforeach
                                </select>
                                <div class="invalid-feedback">
                                    @error('nama')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <select name="gol" id="gol" type="text" onclick="golongan()" class="form-control @error('gol') is-invalid @enderror" value="{{ old('gol') }}">
                                    <option>-- Golongan dan M K G --</option>
                                    @foreach ($golongan as $data)
                                    <option value="{{ $data->tunjangan_gol }}">{{ $data->gol }} | {{ $data->mkg }}</option>
                                    @endforeach
                                </select>
                                <div class=" invalid-feedback">
                                    @error('gol')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <input name="tunjangan_gol" type="number" id="tunjangan_gol" onclick="sum()" placeholder="Gaji Pokok" class="form-control @error('tunjangan_gol') is-invalid @enderror" value="{{ old('tunjangan_gol') }}" readonly>
                                <div class="invalid-feedback">
                                    @error('tunjangan_gol')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <select name="jbt_fungsi" id="fungsi" type="text" onclick="fungsional()" class="form-control @error('jbt_fungsi') is-invalid @enderror" value="{{ old('jbt_fungsi') }}">
                                    <option>-- Jabatan Fungsional --</option>
                                    @foreach ($fungsi as $data)
                                    <option value="{{ $data->tunjangan_fungsi }}">{{ $data->jbt_fungsi }}</option>
                                    @endforeach
                                </select>
                                <div class="invalid-feedback">
                                    @error('gol')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <input name="functional_allowance" type="number" id="functional_allowance" onclick="sum()" placeholder="Functional Allowance" class="form-control @error('functional_allowance') is-invalid @enderror" value="{{ old('function_allowance') }}" readonly>
                                <div class="invalid-feedback">
                                    @error('function_allowance')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <select name="jbt_structure" id="structure" onclick="structural()" class="form-control @error('jbt_structure') is-invalid @enderror" value="{{ old('jbt_structure') }}">
                                    <option>-- Structural & Functional Position --</option>
                                    @foreach ($structure as $data)
                                    <option value="{{ $data->benefits_structure }}">{{ $data->jbt_structure }} | {{ $data->jbt_function }}</option>
                                    @endforeach
                                </select>
                                <div class="invalid-feedback">
                                    @error('jbt_structure')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <input name="structure_allowance" type="number" id="structure_allowance" onclick="sum()" placeholder="Structural_allowance" class="form-control @error('structure_allowance') is-invalid @enderror" value="{{ old('benefits_structure') }}" readonly>
                                <div class="invalid-feedback">
                                    @error('allowance_structure')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                            <div class="form-group">
                                <input name="total_gaji" type="number" id="total" onclick="sum()" placeholder="Total Salary" class="form-control @error('total_gaji') is-invalid @enderror" value="{{ old('total_salary') }}" readonly>
                                <div class="invalid-feedback">
                                    @error('total_salary')
                                    {{ $message }}
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

Is there anything I missed? I think this is correct, maybe my code insertion is wrong...

Thank you for taking the time to review and answer my questions.

P粉360266095
P粉360266095

reply all(1)
P粉128563140

First use the console log to check your value, then also console the sum and then push it to the sum input field. As far as I know your code is fine and works as raw html on my computer. So you might have selected the wrong id, please check.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>
<body>
    <input name="num1" type="number" id="num1" onkeyup="sum()" type="text" placeholder="" class="form-control" value="" />
    <input name="num1" type="number" id="num3" onkeyup="sum()" type="text" placeholder="" class="form-control" value="" />
    <input name="num1" type="number" id="num2" onkeyup="sum()" type="text" placeholder="" class="form-control" value="" />
    <input name="num1" type="number" id="total" onkeyup="sum()" type="text" placeholder="" class="form-control" value="" />

    <script>
        function sum() {
            var txtFirstNumberValue = document.getElementById("num1").value;
            var txtSecondNumberValue = document.getElementById("num2").value;
            var txtThreeNumberValue = document.getElementById("num3").value;
            var result = parseFloat(txtFirstNumberValue) parseFloat(txtSecondNumberValue) parseFloat(txtThreeNumberValue);
            if (!isNaN(result)) {
                document.getElementById("total").value = result;
            }
        }
    </script>
</body>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template