Tips for parsing information from a text file and displaying it on a website in table format

Would like to create a 2x4 table where the first column has a dropdown for selection. Upon choosing an option, the associated data will populate the other 3 columns. I'm new to this, so please bear with me.

Using x, y, z as placeholders, the data will come from a text file. For example, if "rose" is chosen, the temperature, humidity, and moisture data will be displayed in the x, y, z columns respectively.

<!DOCTYPE html>
<html>

  <head>
    <style>
      table {
        font-family: arial, sans-serif;
        border-collapse: collapse;
        width: 100%;
      }

      td,
      th {
        border: 1px solid #dddddd;
        text-align: left;
        padding: 8px;
      }

      tr:nth-child(even) {
        background-color: #dddddd;
      }

    </style>
  </head>

  <body>

    <h2>HTML Table</h2>

    <table>
      <tr>
        <th>Select Plant</th>
        <th>Temp</th>
        <th>Humidity</th>
        <th>Moisture</th>
      </tr>
      <tr>
        <td>
          <select id="mySelect" onchange="myFunction()">
    <option value="daisy">daisy
    <option value="tulip">tulip
    <option value="rose">rose
    <option value="sunflower">sunflower

</select>


          <p id="demo"></p>


          <script>
            function myFunction() {
              var x = document.getElementById("mySelect").value;
              document.getElementById("demo").innerHTML = "You selected: " + x;
            }

          </script>

          <td>x</td>
          <td>y</td>
          <td>z</td>

      </tr>


    </table>

  </body>

</html>

JSFiddle

Answer №1

You can enhance your HTML code by following these steps:

<select id="mySelect" onchange="myFunction(this)">
    <option value="">Select a flower...</option>
    <option value="X1,Y1,Z1">daisy</option>
    <option value="X2,Y2,Z2">tulip</option>
    <option value="X3,Y3,Z3">rose</option>
    <option value="X4,Y4,Z4">sunflower</option>
</select>

Remember to close your tags properly for HTML standard compliance and utilize the "this" pointer to avoid hardcoding IDs in your JavaScript function. This allows for easy reusability of the function for multiple elements.

It's a good practice to include a placeholder option like "Select a flower..." to ensure the onchange event triggers even on page reloads, preventing the loss of initial values in your data table.

For inputting data from a file into the "value" attribute, consider alternative approaches for handling large file sizes, such as implementing paging functionality.

In your JavaScript function:

function myFunction(element) {
    var xyz = element.value;
    var flowerName = element.options[element.selectedIndex].innerHTML;
    // Parse xyz (assuming it's in the format "X,Y,Z")
    // You may also need to parse/format flowerName accordingly
    document.getElementById("demo").innerHTML = "You selected: " + flowerName ;
    document.getElementById("demo-values").innerHTML = "The data: " + xyz;
 }

Answer №2

If you're able to, I recommend storing your data in a JSON file. If this format is new to you, check out this resource: https://www.w3schools.com/jS/js_json_intro.asp

By storing data in a JSON file, it becomes easier to access the data directly in JavaScript and display it on your HTML page.

Once you've familiarized yourself with JSON, let me know if this approach is not feasible!

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

The functionality of $watch in AngularJS is not meeting the desired outcomes

Within my controller, I am looking to receive notifications when the value of a certain variable changes. Specifically, I want a function to be triggered whenever this variable is updated. To achieve this, I am utilizing the $watch method in AngularJS. Her ...

What is the best way to enhance an object using a class in ES6?

In an effort to improve the clarity of my ES6 class definition, my current code looks like this: class SomeClass { constructor({a, b, c, d, e}) { this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; // additional code here ...

Can one jQuery script be used for multiple ajax 'like' buttons?

Hey there! I'm working on an Ajax 'like' button that utilizes jQuery. This button will be utilized multiple times on a single page. I'm looking for a way to streamline the process and avoid including the jQuery script multiple times. Is ...

Contrast between the act of passing arguments and using apply with arguments

I have an important backbone collection that utilizes a save mixin to perform Bulk save operations (as Backbone does not natively support this feature). // Example usage of Cars collection define([ 'Car', 'BulkSave' ], function(Car ...

Angular Universal causing issues with updating the DOM component

@Component({ selector: 'mh-feature-popup', template: ` <div class="full"> <div> <div class="container-fluid" [@featurepop]="state"> <div class="row"> <div class="col-xs-12 col-md-4 col-md-offse ...

What strategy should be employed to ensure that Javascript is operational upon completion of page loading?

I'm facing a challenge with running my JavaScript on my ASP.NET webform page. It seems like the elements are not fully loaded when the script runs, which could be causing issues. How can I use jQuery to ensure that my script is placed at the bottom of ...

What is the best way to convert text into a JSON object?

I have a list of DHCP lease details in a file, here is an example of the entries: lease 172.16.20.11 { starts 4 2014/10/09 18:33:57; ends 4 2014/10/09 18:43:57; cltt 4 2014/10/09 18:33:57; binding state active; next binding state free; ...

What is the best way to remove excess content that falls outside the viewBox?

Is there a function or method to trim a path that extends beyond the viewbox rather than simply concealing it? I am currently using svg-edit, which has a specific viewbox or canvas area. Any elements drawn outside of this canvas remain hidden. However, wh ...

Managing time in an Angular application using Typescript

I am facing an issue with formatting the time obtained from an API in my FormArray. The time is received in the format: 14.21.00 My goal is to convert this time to the following format: 2:21 PM I have attempted to format it using Angular's DatePip ...

How can I implement an AJAX request with MongoDB in Node/Express?

Let's begin with a simple webpage: an HTML Form, a button, and a div-box. When the button is clicked, the Form data will be sent via AJAX. The data will then be stored in MongoDB and retrieved into the div-box seamlessly without any page refresh. A ...

Is your Ajax response suddenly failing to work after the initial attempt?

Describing my predicament: The code snippet below is what I have been using to insert a custom-designed div into my webpage. Initially, the div is successfully added; however, it stops working after the first instance. $('#addanother').click(fu ...

Implementing state management with Vue.js

After creating a login page and setting conditions to display different NAVBARs based on the user's login status, I encountered an issue where the rendering seemed to be delayed. In the login process, I utilized local storage to store a token for auth ...

Pass a variable between popup.js and background.js in a Chrome Extension

Despite finding some answers to similar questions, none of them provide a complete solution. The information in the answers lacks necessary documents that were not included in the original question. My goal is to create a scaffold for Chrome extensions wh ...

Facing difficulty in submitting form data with Reactjs

I am currently working on a project using Reactjs with nextjs. I am facing an issue where I am unable to receive any response or see anything in the console.log when trying to post form data. I have tried implementing the following code in index.js, but ...

Vue3 is struggling to apply dynamic styling exclusively to certain buttons

Currently, I am diving into the world of vue and facing a challenge in styling buttons dynamically when clicked individually. These buttons serve as filters for a list of products, and my goal is to apply one style when the filter is 'on' and ano ...

Having trouble adjusting the label fontSize in ReactJS when using semantic-ui-react?

Is there a way to decrease the size of the label for a form input? In this scenario, attempting to set the fontSize does not work: <Form.Input label="Username" style={{fontSize: '10px'}} /> Does anyone have any suggestions on how to res ...

Tips for preventing the automatic insertion of tbody tag by Firefox?

My webpage is being rendered client-side using XSL and XML sent by the server. I'm encountering a problem with Firefox adding an implicit tbody tag. The issue arises because my XSL generates certain tbody tags based on certain conditions. However, Fi ...

How is it that connecting an event listener to a React child component can be done in a way that resembles simply passing a prop

class Board extends React.Component { constructor(props) { super(props); this.state = { squares: Array(9).fill(null), }; } handleClick(i) { // do things } render() { return ( <div ...

Accessing values from an array within a JSON object using jqGrid

Here is an example of my JSON data: [{"codDiretor":"123", "nomeDiretor":"Nome do Diretor", "data":"29/01/2014", "documentos":[{"codDocumento":"1", "nomeDocumento":"Primeiro Doc"}, {"codDocumento":"2","nomeDocumento":"Segundo Doc"}] ...

Utilize the bodyParser middleware within a middleware to handle the request body parsing

Currently, I am developing an Express app where most routes have a limited body size except for admin users who require unlimited body size for certain routes. To manage this, I implemented a middleware called limitBodySize using the following syntax: app. ...