Utilize JavaScript to retrieve the content of an input box, and subsequently input text into two different textboxes based on the value obtained

In my college assignment, I'm working on a website about the game of rock, paper, scissors, lizard, Spock from The Big Bang Theory. The requirements are to use HTML, CSS, and JavaScript only, so please don't suggest JQuery as it's not allowed. While my HTML and CSS are fine, I need help with incorporating JavaScript, which is new to me.

I've set up a combo box with options for Rock, Paper, Scissors, Lizard, and Spock. Below that, I have three text boxes on the page. Currently, I'm able to use JS to get the selected value from the combo box and display it in the middle box. However, I'd like to also show what beats the selected option in the first box and what it beats in the third box. For example, if "Rock" is chosen, "Paper" should go in the first box and "Scissors" in the third box.

I want these values to update dynamically based on the selection from the combo box. Here's the HTML structure so far:

 <!Doctype html>
<html>

<head>
<link rel="stylesheet" type="text/css"
href="Web.css">
<script>
function Choice()
{
var mylist=document.getElementById("myList");
document.getElementById("Choice").value=mylist.options[mylist.selectedIndex].text;

var x= Choice.value;

}
</script>

...

</body>
</html>

Your guidance would be appreciated. Thank you, Chris

Answer №1

Consider changing the title

Utilize javascript to retrieve the dropdown value and dynamically fill other input fields based on conditions.

Below is a demonstration of the script in action:

<script>
function execute() {
    var resultTable = analyze();
    console.log(resultTable);
    
    document.getElementById("srt").value = document.getElementById("Ultra").value;
}

function analyze() {
    var selectedValue = document.getElementById("srt").value,
        resultTable;
    
    if(selectedValue === 'rock') {
        //assign values based on result table
    }
    else if (selectedValue === 'paper') {
        //assign values based on result table
        var table = [{ "first":"Paper" , "second":"Rock", "third":"Scissors", "fourth":"Lizard", "fifth":"Spock"}];
        console.log(table);
        return table;

    }
    else if (selectedValue === 'scissors') {
        //assign values based on result table
    }
    else if (selectedValue === 'lizard') {
        //assign values based on result table
    }
    else if (selectedValue === 'spock') {
        //assign values based on result table
    }    

}

</script>

Take a look at this fiddle example

There are only 5 possible outcomes.

  1. Create a result table for your outcomes.

  2. Enter the data for each table as json in the table variable within the corresponding if statement.

  3. Return the table as resultTable variable

  4. Go through the data and assign values to the appropriate input IDs

    Example: //Assign a value for each input document.getElementById("srt").value = resultTable[0].value; document.getElementById("second").value = resultTable1.value; document.getElementById("third").value = resultTable2.value; document.getElementById("fourth").value = resultTable[3].value; document.getElementById("fifth").value = resultTable[4].value;

This example should guide you on what needs to be done, while also helping you understand the process.

NOTE: The example in my code executes onClick, which may not be suitable for your scenario. You will need to use a different trigger. Included links to assist others facing similar challenges.

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

How to effectively pass custom props or data to the Link component in Next JS

As I dive into Next JS, I've hit my first roadblock. Currently, I am working on a page that showcases various podcast episodes with preview cards on the homepage. The card component code looks like this: import React from 'react'; import Li ...

Troubleshooting: Twitter Bootstrap dropdown feature experiencing issues within navigation bar

I am facing an issue with getting the bootstrap dropdown to function properly. I have tested it on my mobile device and noticed that the menu does not drop down as expected. Here is a DEMO Below is the code snippet: <nav class="navbar navbar-inverse ...

Guide to switching a button using JavaScript

I am trying to create a button that will toggle the font size between 16px and 14px when clicked. The button text should also change from "Increase Font" to "Decrease Font" accordingly. UPDATE: I managed to get it working, but it only toggles twice and th ...

The H1 tag fails to appear on the webpage

I can't figure out why my H1 is not visible. It seems to be hidden by a div. What's strange is that the parent div is visible when I change the background color to something other than transparent. Even an input tag within the same div displays c ...

Locate the element for a button or an image

Having trouble recognizing the object in the image. Looking for guidance on how to identify the Button using ClassName or customized XPATH. <div class="schedule"> <a href="https://link" target="_blank"> <img border="0" alt="Sc ...

Update the Vue component upon fetching new data

How can I continuously refresh the list of items when a button in a sibling component is clicked? The watch method only triggers once, but I need it to constantly refresh. This is the parent element: <template> <div class="container"& ...

Merge two JSON objects together by matching their keys and maintaining the original values

After having two separate JSON objects representing data, I found myself in the position of needing to merge them into one combined result. The initial objects were as follows: passObject = { 'Topic One' : 4, 'Topic Two' : 10, &apos ...

Tips for incorporating an alt attribute into image tags using jquery

I recently inserted a Google map into a webpage and noticed that it generated multiple img tags without the alt attribute. I'm looking for a way to dynamically add the alt attribute to each img tag within a div using jQuery. Can someone suggest an alt ...

Box-sizing:border-box causing CSS padding problem in Firefox

Something strange is happening in Firefox, it seems like the debug console is not telling the truth or there's something I'm not seeing. Here's the CSS for the body tag: html, body { width: 100%; } body { padding: 5% 10% 0 10%; ...

How to transform multi-dimensional arrays to JSON format using JavaScript (and maybe jQuery)

Currently facing a Javascript dilemma where data storage is essential in the following format: MainArray(Array(JavaScript Object, JavaScript Object, etc etc..), Array(JavaScript Object, JavaScript Object, etc etc..), etc etc..) The main array consists of ...

What is the most effective way to include JavaScript code in a PDF file?

What is the process for integrating JavaScript code into a PDF document? I am familiar with coding in JavaScript and would like to learn how to add it to a file in order to perform tasks such as displaying the current date or using a combobox. ...

Keep the animation keyframes looping endlessly

I am creating a countdown timer circle. The animation functions properly on the initial iteration, however, the circle animation remains full after the first iteration and does not reset. Despite this, the countdown number continues to function correctly, ...

When items are removed client-side, the Listbox becomes null

Given a Web Forms project inherited by me, I am relatively new to the field of Web development. The page in question features 2 listboxes: lstCaseLoad, containing "Caseloads" (ID numbers), and lstAssignedCaseLoad, filled with Caseloads chosen by the Form U ...

Adjust the height of the list when including or excluding floated list items that are not in the final row

Within my ul are li elements with varying widths that wrap around inside a container into multiple rows. When removing an li element from the ul, I want the container's height to adjust dynamically in case a row is eliminated. If the removal happens ...

Leveraging Firebase for data storage in creating XML files

According to my understanding of the firebase documentation, data that is inputted is converted into JSON format. The easiest way to connect to firebase is through the use of javascript. However, is it feasible to utilize the JSON data in firebase to gen ...

JavaScript Troubleshooting: Fixing the "Uncaught error: undefinedModule" Issue

Currently, I'm utilizing a Javascript file in an attempt to acquire a token from ArcGIS Online. Unfortunately, each time I give it a shot, the following error is returned: init.js:11 Uncaught Error: undefinedModule The Javascript file (GetAToken.js) ...

Leveraging server-sent events (SSE) for real-time updates on a web client using JavaScript

I have been experimenting with server-side events (SSE) in JavaScript and Node.JS to send updates to a web client. To simplify things, I created a function that generates the current time every second: setTimeout(function time() { sendEvent('time&a ...

Verify the correctness of two password fields using jQuery

I need help finding a script that can actively check if the passwords entered in two password fields match while typing. The script should indicate if the passwords do not match without needing to click the Submit button. Below are the two password fields ...

The AngularJS page on my website is currently stuck in the mobile version, and strangely, the Google Chrome console is

When browsing our AngularJS website in mobile view on Google Chrome, it gets stuck and becomes unresponsive. The developer console does not show any error messages during this time. To replicate the issue, switch to mobile view, click on the "All top compa ...

Error: unable to locate the react-redux context value; make sure the component is enclosed in a < Provider > tag

import React, { Component } from 'react' import { createStore } from 'redux' import { Provider, connect, useSelector } from 'react-redux' function rootReducer(state = { name: 'store' }, action) { return state ...