My goal is to provide flexibility in updating values through the use of radio buttons, dropdown menus, lists, and other input methods

var trees = [];
        trees["Furu"]  = {1915: 20, 1950: 31, 1970: 53, 1990: 89, 1995: 102, 2000: 117};
        trees["Gran"]  = {1915: 23, 1950: 39, 1970: 72, 1990: 89, 1995: 92, 2000: 99};
        trees["Lauvtre"] = {1915: 4, 1950: 6, 1970: 8, 1990: 12, 1995: 16, 2000: 18};

tree = trees["Gran"];

"Gran", "Furu" and "Lauvtre" represent different types of trees. To change the type, you can use buttons, a dropdown menu, or a list to make a selection and retrieve the values from the corresponding arrays.

For example, you can choose between "Gran", "Furu", and "Lauvtre" buttons, and upon clicking one of them, you will receive the same information as if you had manually set tree = trees["Gran"] in your code.

<!doctype html>
<html>
    <head>
        <title>Intro JavaScript</title>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <style> 
            p {font: 20px arial, sans-serif;}
        </style>
        <script>
        window.onload = oppstart;
        //Adapted from Amund
        var trees = [];
        trees["Furu"]  = {1915: 20, 1950: 31, 1970: 53, 1990: 89, 1995: 102, 2000: 117};
        trees["Gran"]  = {1915: 23, 1950: 39, 1970: 72, 1990: 89, 1995: 92, 2000: 99};
        trees["Lauvtre"] = {1915: 4, 1950: 6, 1970: 8, 1990: 12, 1995: 16, 2000: 18};
        gjennomsnitt = [];
        var years = [1915, 1950, 1970, 1990, 1995, 2000];       
        var tree; 

        function oppstart(){
            document.getElementById("btnVis").onclick = vis;
            tree = trees["Gran"]; //To handle gran[]
        }
        function vis(){
            var tekst = "Increase per period:</br> </br>";
            for (var i = 0; i < years.length-1; i++){
                tekst += years[i] + " - " + years[i+1] +' <i class="fa fa-arrow-right"></i> ' + (tree[years[i+1]]- tree[years[i]]) + " million trees" + "</br>";
            }
            for(var i = 0; i < years.length-1; i++){
                gjennomsnitt.push((tree[years[i+1]]- tree[years[i]])/(years[i+1] - years[i]));
            }

            var highest = 0;
            for(var i = 0; i < gjennomsnitt.length; i++){
                highest = gjennomsnitt[i] > gjennomsnitt[highest] ? i : highest;
            }

            document.getElementById("utskrift").innerHTML = tekst + "</br>" +
            "The period with the strongest average growth was between " + years[highest] + " - " + years[highest+1];
        }   

        </script>
    </head>
    <body>
        <h1>Development of spruce trees in Norway</h1>
        <button id="btnVis">Show data</button>
        <p id="utskrift"></p>
    </body>
</html>

Answer №1

Hopefully, I have understood your query correctly. Does the following code snippet meet your requirements?:

FIDDLE

Here is the HTML code:

<div>Furumuru
  <input type="radio" data-key="Furumuru"></input>
</div>
<div>Granmran
  <input type="radio" data-key="Granmran"></input>
</div>
<div>Lauvtremauvtre
  <input type="radio" data-key="Lauvtremauvtre"></input>
</div>
<div id="result"></div>

And here is the JavaScript code:

var inputs = Array.prototype.slice.call(document.getElementsByTagName("input"));
function processInput(e){
    inputs.filter(function(d,i){return d!==this},this).forEach(function(d,i){d.checked = false;})
    document.getElementById("result").textContent = JSON.stringify(processInput.__data[this.dataset.key]);
}
processInput.__data = {
    Furumuru:{1915: 20, 1950: 31, 1970: 53, 1990: 89, 1995: 102, 2000: 117},
    Granmran:{1915: 23, 1950: 39, 1970: 72, 1990: 89, 1995: 92, 2000: 99},
    Lauvtremauvtre:{1915: 4, 1950: 6, 1970: 8, 1990: 12, 1995: 16, 2000: 18}
};

inputs.forEach(function(d,i){
    d.addEventListener("change",processInput,false);
})

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

Using Javascript and jQuery to create an infinite animated background change with fade effect

I am struggling to figure out how to automatically change the background of a div every 3 seconds, looping through a series of images. I posted about this issue before but didn't receive much help. function animate() { change1(); change2() ...

Querying the children of an element using jQuery

$('.Title').children(function() { var titleValue = $('.Title').text(); console.log(titleValue); }); Wondering how to access all children with the 'title' tag and log their values individually. ...

AngularJS - Unusual outcomes observed while utilizing $watch on a variable from an external AngularJS service

Within the constructor of my controllers, I execute the following function: constructor(private $scope, private $log : ng.ILogService, private lobbyStorage, private socketService) { this.init(); } private init(){ this.lobbyData = []; this.initial ...

Determining the most recent array within an array object in PHP

My goal is to extract data from a JSON object, transform it into an array of objects, and then determine the latest array within this structure based on a specific set of values. For example: // Array structure [ [response] => [ [0] => [ ...

The latest version is available, but remember to update @react-navigation/bottom-tabs, @react-navigation/stack, and @react-navigation/drawer to at least version 5.10.0

As a newcomer to react-native, I am currently attempting to execute a program using expo but encountering a yellow error message. The error states: 'It seems that you are utilizing an outdated version of the react-navigation library. Please ensure th ...

Remove redundant values within multi-layered arrays

Looking for help to remove duplicates from the mcs_client_ec_no field in the array below, but struggling to find a solution. Array ( [1] => Array ( [mps_client_ec_no] => REG5 [mcs_client_ec_no] => GL98888 [mps_schoo ...

How to extract a specific field from an object in an array using MongoDB's $first operator, excluding the use of $getField

I am currently working with mongo version 4.4 and I need to achieve a specific result in my projection: { districtId: 'xkj1', firstCollegeId: '123' } My goal is to only retrieve the FIRST element that is classified as type: ' ...

Use JavaScript to limit Google Analytics and Yandex.Metrica to track only the referral sources and screen sizes

I prefer not to include external JavaScript on my website for unnecessary tracking purposes. However, I do need to gather referrer and screen size information, which cannot be achieved with a regular HTML img tag alone. What is the most standard-complian ...

Placing a cookie using nookies within the Next.js API directory

I'm currently facing an issue while trying to use the nookies npm package to set a cookie within the Next.js api folder. I've successfully set up a cookie using the same code with nookies before, but for some reason, it's not working in this ...

What is the process for updating the h1 header with text entered into an input field?

I'm working on an assignment that requires me to change the h1 heading to reflect whatever is entered into the input field. To accomplish this, I need to create a function using getElementByID. Here's what I've done so far: <!DOCTYPE htm ...

Adjusting Text Size Depending on Width

I recently used an online converter to transform a PDF into HTML. Check out the result here: http://www.example.com/pdf-to-html-converted-file The conversion did a decent job, but I'm wondering if it's feasible to have the content scale to 100% ...

I built a custom Angular application integrated with Firebase, and I'm looking for a way to allow every user to have their individual table for managing tasks

I am looking to enhance my code so that each user who is logged in has their own tasks table, which they can update and delete. Additionally, I need guidance on how to hide menu items tasks, add-tasks, logout for users who are not logged in, and display th ...

Tips for restricting User access and displaying specific sections of the menu

I have a component that utilizes map to display all menu parts. Is there a way to make certain parts of the menu hidden if the user's access rights are equal to 0? const Aside: React.FunctionComponent = () => { const[hasRight, setHasRight] = us ...

tips for implementing JSON loading in Ext JS

While attempting to load values from a web service, I encountered the following error message: "ChatStore.data.items[i] is undefined." The mapping code for extjs is as follows: ChatStore = new Ext.data.JsonStore({ storeId: 'ChatStore1' ...

Setting a default image for the img setAttribute: A step-by-step guide

My code involves creating an img element using JavaScript: var photo = document.createElement("img"); This element is populated with photos whose names are stored in an array of objects. The images are loaded like this: photo.setAttribute('src&apos ...

Arranging JSON based on values in an array using node.js

Seeking assistance with sorting a JSON object by specific keys within a node.js program. The task involves arranging the object in ascending order based on the "trip_miles" value when the "trip_name" equals CC, and then extracting only the id's as out ...

Combining 2 JSON Outputs and Calculating Occurrences of Certain Values

Having multiple JSON objects like these: ITEM 1: { "countries_views": [ { "thecount": "563", "country": "Greece" }, { "thecount": "48", "country": "United States" }, ...

Using three.js to input text instead of particles within a particle cloud

I have a unique three.js codepen project where square particles drift through the space. However, I am now looking to enhance it by incorporating text (perhaps using geometry?) instead of the square particles, creating a word/tag cloud effect. Is this eve ...

Ensure that the jQuery Knob is set to submit any modifications only after the final adjustment

Utilizing jQuery Knob by anthonyterrien, I have configured the step to be 20. My goal is to transmit the knob's value to the server. The issue arises when the change function is triggered every time the user adjusts the knob using either a right or le ...

Implement Material UI TextField to ensure all required fields are properly formatted

Looking to customize the underline border color of TextFields marked as mandatory within a form using react-hooks-form. I understand that I need to define a style for these fields, but I'm struggling with where to start... This is the current code s ...