What is the best way to handle variables in javascript?

After making an AJAX request, I expected the results to display when I select from the drop down menu. However, I encountered a problem where only a table is being displayed. I am confused as to why the data is not coming from the variables $grade (calculation of grade) and $value (calculation of total value).

Upon trying to retrieve the element $grade, I found that it is undefined.

<script type="text/javascript>
    $(document).ready(function(){
        $('#kategori').on('change', function(e){
            var id = e.target.value;
            $.get('/khs/khs_semester/' + id, function(data){
                console.log(id);
                console.log(data);
                $('#khs').empty();
                $.each(data, function(index, element){
                    $('#khs').append("<tr><td>" + element.kode_mk + "</td><td>" + element.nama_mk + "</td>" + "<td>" + element.semester + "</td><td>" + element.jml_sks + "</td><td>" + element.$grade + "</td></tr>");
                });
            });
        });
    });
</script>
<table class="table table-bordered>
    <thead>
        <tr>
            <th width="100">KODE MK</th>
            <th width="350">NAMA MK</th>
            <th width="50">SEMESTER</th>
            <th width="50">SKS</th>
            <th width="50">GRADE</th>
        </tr>
    </thead>
    <tbody id="khs">
        @foreach($mahasiswa as $row)
            @php

                $nilai = hitung_nilai($row->id);
                $grade = hitung_grade($nilai);
                $mutu  = hitung_mutu($grade);

            @endphp
            <tr>

                <td>{{ $row->kode_mk }}</td>
                <td>{{ $row->nama_mk }}</td>
                <td>{{ $row->semester }}</td>
                <td>{{ $row->jml_sks}}</td>
                <td>{{ $grade }}</td>
                <td>{{ $mutu*$row->jml_sks }}</td>
            </tr>
            @php
                $totalSKS=$totalSKS+$row->jml_sks;
                $totalMutu = $totalMutu+$mutu*$row->jml_sks;
            @endphp
        @endforeach
    </tbody>
</table>

https://i.sstatic.net/WDXPZ.png

Answer №1

If you want to make it easier to identify each row, consider assigning an ID to each row. You can use an ID in the <tr id="KM003"> tag or in the specific TD element for the grade, such as <td id="KM003Grade">. This way, you can retrieve the grade based on the student's code, like this:

var Name = $('#IDused').innerHTML;

Alternatively, you can use a class in each child TD to distinguish the attributes. For example:

<tr id="km003">
<td class="kode">{{ $row->kode_mk }}</td>
<td class="nama">{{ $row->nama_mk }}</td>
<td class="semester">{{ $row->semester }}</td>
<td class="jml_sks">{{ $row->jml_sks}}</td>
<td class="grade">{{ $grade }}</td>
</tr>

Then, you can retrieve the value using:

var grade = $("#km003 .grade").innerHTML;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transferring information to a controller using ajax in ASP.NET Core

I am encountering an issue with sending data to the controller through ajax. The value goes as "null". Can someone please assist me with this? Here are my HTML codes: <div class="modal fade" id="sagTikMenuKategoriGuncelleModal" data ...

What is the function type in React-Native?

I need assistance understanding the following function: const myFunction = () => () => { //perform a task } Can someone explain the meaning of this function? ...

Instructions for removing all entries from a DynamoDB table

I am facing a challenge with deleting multiple items from a DynamoDB table. While the documentation suggests dropping and recreating the whole table, I want to avoid this approach as my table was created using AWS Amplify and I don't want to risk disr ...

Create a discord.js bot that can randomly select and send a picture from a collection of images stored on my computer

I'm currently working on a bot that sends random pictures from an array of images stored on my computer. However, I encountered an issue when trying to embed the image, resulting in the following error message: C:\Users\47920\Desktop&bs ...

Can someone explain why my table in Laravel is only showing the third set of data and not the others?

I'm facing an issue with displaying all of the data in my table named taglineImageCarousel. The problem lies in the slide_ID column, where it is being populated with data from slide-01, slide-02, and slide-03. Below is the code snippet: taglineImage ...

The width of table columns is altered upon the second rendering in JQuery DataTables

Is there a way to prevent JQuery DataTables cells from extending width after adding hyperlink to cell? I am facing an issue where I have two tables on the same page. The first table column has a link which populates the second table. It works fine when th ...

Upon initiating a second user session, NodeJS yields contrasting MySQL outcomes

As a novice in NodeJS and Express, I find myself stuck on what seems to be a trivial issue. Despite my best efforts, I have been unable to resolve the problem on my own. My aim is to create a basic web application that allows user authentication and displ ...

Using Vue js to scrollIntoView will only affect the specific element's div

I am currently working on a Vue page that consists of two divs. The Select-Div contains a list of elements, and when I click on an element from the list, the Scroll-Div is supposed to scroll to the corresponding ID element inside it. Ideally, the scrolling ...

Unusual navigation patterns in Angular

https://i.sstatic.net/kdh4A.png My Mean app is set up with the Angular app residing in the '/dashboard' path. The home page of the app works fine. Reloading the app returns the page it was on. Even routes with '/dashboard/anything/anything& ...

Unlock the capability to automatically swipe on a React Native TextInput Carousel while the user types

My challenge involves setting up a carousel with either Flatlist or ScrollView (I have tested both options). This Carousel consists of multiple TextInputs. The goal is to achieve the following: There are 4 TextInputs. When the user enters 6 digits in the ...

Django enables anonymous Ajax requests to reach a Generic View

Here is the current progress I have made: from django.views.generic import View from django.views.decorators.csrf import csrf_exempt class ConfigurationView(View): @csrf_exempt def dispatch(self, *args, **kwargs): return super(Configurati ...

Anonymized Score Reporting through Google Analytics analytics.js

Recently, I've been working on setting up Google Analytics for tracking my web game. Everything seems to be working fine, except for one issue - when I check my custom report the next day after testing the game, I notice that all the scores have been ...

When utilizing jQuery $.post to send hidden input values, there may be the potential for empty database columns and encountering an

I have encountered an issue where I can view the values passed in the console, but nothing gets stored in the database and I keep getting undefined errors as output. Rather than attributing this error to my JavaScript code, I believe the problem lies in ho ...

Using ng-style to apply a background image with a dynamic id

Hey there, I'm facing an issue here. The link provided below seems to not be working properly. Even though the id is set correctly within the scope, it seems like there might be a parsing error occurring. Any thoughts on what might be causing this pro ...

Experiencing issues in retrieving data post-login using ASP.net session key method

I have developed a website using AngularJS for the front-end and Windows webforms for the back-end. I believe that the Authorization process is carried out using ASP.net Session Key. The approach involves creating an AngularJS Post method for "login" foll ...

Leveraging onClick in combination with React's AutoBind

Just started learning React, so please point me towards documentation if I missed anything. I'm attempting to initiate an Ajax call using the onClick method following a tutorial on React. See the code snippet below. doSomething: function() { // ...

Second attempt at loading Jquery was unsuccessful

I have a question regarding the code snippet below that I am using to reload a PHP file after 20 seconds within a div. However, the issue I am facing is that after the initial load in my target div, it does not display anything. Do you have any suggestion ...

Tips for storing and recalling a 24-hour countdown timer using Local Storage

I'm new to JavaScript and I have a 24-hour countdown timer that resets on page reload. However, I want to use LocalStorage to save the starting progress so that it continues running even if the page is closed or refreshed. The goal is for the timer to ...

Why is it that I may sometimes have difficulty reading the undefined property 'geometry'?

I am currently working on a website that allows users to submit a form with an address for Google Maps integration. However, I have been encountering a strange issue with the Google Maps API. Occasionally, I receive the error message "Cannot read property ...

Split a string containing integer and decimal values into an array

I encountered an issue when converting a string to an array: var data = "[[1, 2, 3,], [3, 2, 1]]"; var array = JSON.parse(data.replace(/,\s*]/g, ']')); This problem arises when handling floating point numbers in the input: var data = "[[2 ...