Tips for updating and emphasizing the value in a column

Are you looking for a way to input scores and calculate the average score? You can use a form where users can enter their scores, click submit, and see a table of previous scores along with two buttons. The average score column will initially show a question mark ("?"). By clicking on the "Calculate the average score" button, the system will compute the average scores and replace the question mark value. Additionally, if any average score is greater than or equal to 8, the corresponding cell will be highlighted in red.

If you need assistance with calculating the average scores and updating the values, here is an example code snippet:


<!-- HTML code goes here -->

And within your JS file:


// JavaScript logic for computing averages
var testScore = {
    name: "",
    math: 0,
    physical: 0,
    chemistry: 0,
    avg: 0
};

var i = 1;

$(function() {
    $('#show_table').click(function() {
        // Handle displaying tables and capturing user inputs
    });
});

// Functionality to calculate average scores and update display

// Code snippet ends here

You can also format your CSS accordingly to ensure proper alignment and responsiveness:


/* CSS styling for responsive design */
#divTable {
  display: none;
  // Add more CSS properties as needed
}
@media only screen and (max-width: 768px) {
    /* Media query rules for mobile view */
}

@media only screen and (min-width: 768px) and (max-width: 991px) {
    /* Media query rules for tablet view */
}

@media only screen and (min-width: 992px) {
    /* Media query rules for desktop view */
}

Answer №1

To enhance the functionality of your table, consider adding classes to the score and average elements for easier targeting with jQuery.

$(function() {
    $('#show_table').click(function() {
        $('#divTable').css("display","block");
        var name = $("#name").val();
        var math = $("#math").val();
        var physics = $("#physics").val();
        var chemical = $("#chemical").val();
        $('#divTable').css("display","block");
        
        $("#name").val("");
        $("#math").val("");
        $("#physics").val("");
        $("#chemical").val("");

        var html = "<tr>";
        html += "<td>" + i + "</td>";
        html += "<td>" + name + "</td>";
        html += "<td class='score'>" + math + "</td>";
        html += "<td class='score'>" + physics + "</td>";
        html += "<td class='score'>" + chemical + "</td>";
        html +=  "<td class='average'>?</td>";

        
        $('#divTable table').append(html);
        i++;
    });
    $('#showAvg').click(function() {
       var $scores = $('#divTable').find('.score');
       var score = $scores.map(function() {
          return parseInt($(this).text(), 10);
       }).reduce(function(sum, score) {
          return sum + score;
       }, 0);
       var average = score / $scores.length;
       $('.average').html(average);
    });
});

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

Issue with CSS floating elements on left or right causing a significant gap

I've encountered an issue with my CSS layout. I have specified "float:right; clear:right" for ads and "float:left" for blocks in the main text meant to flow like images. However, there is a significant gap in the main text flowbox. The main text only ...

Error in designing the ReactJS navbar (utilizing internal Bootstrap CSS)

I am facing an issue with the navigation bar in my project. When I use a link to an external source for the navigation bar, it works fine. However, when the internet connection is slow, I notice a brief loading time for the navigation bar which is quite bo ...

Abstract forms on the canvas are dancing in a strange and unusual rhythm

As a beginner in web design, I've made progress but still have questions. Can someone help explain this to me? Here is how a specific section on my website is set up: https://i.sstatic.net/8ntpR.png I'm working on splitting the screen with 40% ...

Tips for enhancing this CSS design to resemble a table

I am a beginner in working with CSS layouts for websites and I have implemented the following code that serves my purpose. Although it is currently functional, its functionality does not necessarily mean that it is well-written. Could someone review this ...

What could be causing the malfunction in my Bootstrap navbar?

<!-- Including Bootstrap CSS files locally-->> <link rel="stylesheet" href="bootstrap.min.css"> <link rel="stylesheet" href="bui.css" <!-- Creating a Navbar--> <nav class="navbar navbar-expand-lg navbar-light bg- ...

Strangely Bizarre CSS Quirk

At this website, there seems to be an issue with how the footer displays on Internet Explorer and certain older versions of Firefox. However, it appears perfectly fine on Firefox 3.6.13. Any suggestions on what could be causing this discrepancy? ...

Displaying an Instagram image using an HTML image tag

After entering the URL in my browser, I can clearly view the photo on However, when trying to use the same URL within an html img tag, all I see is a broken image on my screen! <img src="https://www.instagram.com/p/CBqFrrCHVM7/media/?size=t" ...

Avoid letting words be separated

My question involves HTML code <div class="col-md-4">...</div> <div class="col-md-4">...</div> <div class="col-md-4">Lorem Ipsum</div> When I view this on a mobile device, the text appears as follows: Lorem Ip- sum I ...

What could be causing the error? And is there a way to successfully append the child div to the parent div?

Here is the error message that I am encountering: Uncaught TypeError: Cannot read property 'appendChild' of null at HTMLButtonElement.<anonymous> Error is located at app.js on line 7 const flexContainer = document.querySelector(&apo ...

Java Function for Cleaning HTML Id Attributes

Looking for a more elegant solution to sanitize HTML ID attributes? /** * * HTML 4 Specification * ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number * of letters, digits ([0-9]), hyphens ("-"), underscores (" ...

What is the best way to retrieve the previously chosen value from one dropdown and populate it into another dropdown when

I have 3 choices available. When the user selects the third option, a jQuery onchange event is triggered to send the variable to another PHP file for database validation based on the selected id. The challenge lies in how I can also include the variables f ...

Is there a way to hide a paragraph or div using javascript?

I am experimenting with using buttons to hide paragraphs using javascript, but even when I set them as "hidden", there is still excess blank space left behind. Is there a way I can eliminate that extra space? Below is the javascript code: function backgro ...

Navigating between windows in Selenium with Webdriver - Handling unnamed child windows

Currently, I am conducting acceptance testing using webdriver and codeception. This is a relatively new area for me, so your patience is much appreciated. My current challenge involves switching to a child window that pops up after clicking a button: < ...

Leveraging .closest and .find for accessing values for manipulation

Hey there, thanks in advance for your help! I'm wondering if I'm on the right track by using .closest and .find to extract values from the input field .cab. The goal is to take those values and then show the appropriate door size in the DIV .cd ...

Make a JavaScript request for a page relative to the current page, treating the current page

When accessing the page /document/1, the request $.getJSON('./json', ... is sending a request to /document/json I'm interested in requesting /document/1/json Is there a method available to automatically resolve this path without having to ...

Issues with POST functionality using jQuery and Express

I've done extensive research, but I haven't come across anyone facing the same issue as me. Currently, I am attempting to send a POST request using jQuery to my Express server. Here is my code: jQuery: $.ajax({ url: '/', typ ...

Customizing the label styles within MUI's Chip component

Incorporating React with MUI's library has been a seamless experience for me. One of the key MUI components I have integrated is the Chip Within this particular Chip, there lies the label attribute, offering the option to showcase a text f ...

Utilizing Bootstrap 3: Mastering the Implementation of .col Classes with .form-control Classes

In my form, I have utilized the .form-control classes for the form elements. To arrange two inputs side by side, I am applying .col-md-6 on each of these inputs. However, the width generated by .col-md-6 is being overridden by the .form-control class. Is ...

Fill the list items with values from the given array

I have an array called var array = ["one", "two"]; and I want to display each element in a separate list item. The desired output is: <li>Number one</li> <li>Number two</li> However, the current output is: <li>Number onetw ...

Adjust the width of content within a wrapper using HTML and CSS to automatically resize

I am facing an issue with a content div that has variable length content arranged horizontally. I want the width of this content div to adjust automatically based on the length of its content. Removing the arbitrary "20%" value from the width property is c ...