JavaScript dropdown menu that dynamically changes colors

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var redtoggle=false;

function togglered() 
{
redtoggle = !redtoggle;
if (redtoggle)  
{
    document.getElementById("txtInput").style.color = "red";
}
else 
{
    document.getElementById("txtInput").style.color = "black";
}
}
 var bluetoggle=false;

function toggleblue() 
 {
bluetoggle = !bluetoggle;
if (bluetoggle)     
{
    document.getElementById("txtInput").style.color = "blue";
}
else 
{
    document.getElementById("txtInput").style.color = "black";
}
}

 var greentoggle=false;

 function togglered() 
{
greentoggle = !greentoggle;
if (greentoggle)    
{
    document.getElementById("txtInput").style.color = "green";
}
else 
{
    document.getElementById("txtInput").style.color = "black";
}
}
</script>

 <select id="dropdown">
 <button onclick="myFunction()" class="dropbtn">color change</button>
 <div id="myDropdown" class="dropdown-content">
 <option id="disabledselected" >color change</option>
 <option id="btnTogglered" onclick="togglered()">red</option>
 <option id="btnToggleblue" onclick="toggleblue()">blue</option>
 <option id="btnTogglegreen" onclick="togglegreen()">green</option>
 </div>
 </div>

<form>
 <div>
 TEXT INPUT
 <br>
 <input type="text" name="txtInput" id="txtInput">
 </div>
 </form>
 </body>
 </html>

Need some assistance with changing text color in a word possessor I'm working on. The text format changes are functioning properly, but the color change is not. Seeking guidance to identify and rectify the issue.

Answer №1

Keep things simple whenever possible.

function changeColor(c) 
{
    document.getElementById("txtInput").style.color = c;
}
<form>
<select id="dropdown" onchange="changeColor(this.options[this.selectedIndex].value);"> 
  <option value="black">black</option>
  <option value="red">red</option>
  <option value="green">green</option>
  <option value="#FF00F1">another</option>
</select>
<br>
Input text:
<input type="text" id="txtInput" value="test">
</form>

Answer №2

There seems to be a mistake in your JavaScript code. You have accidentally defined the togglered function twice. Please make sure to change the second one to your togglegreen function.

Answer №3

It appears that your JavaScript code is correct, but why do you have this in between your select options?

<button onclick="myFunction()" class="dropbtn">color change</button>
<div id="myDropdown" class="dropdown-content">

To improve your HTML code, consider making the following changes:

<select id="dropdown"> 
  <option id="disabledselected" >color change</option>
  <option id="btnTogglered" onclick="togglered()">red</option>
  <option id="btnToggleblue" onclick="toggleblue()">blue</option>
  <option id="btnTogglegreen" onclick="togglegreen()">green</option>
</select>
 <button onclick="myFunction()" class="dropbtn">color change</button>
 <div id="myDropdown" class="dropdown-content"></div>

        function changeColor()
        {
            
                var txtInput = document.getElementById("txtInput");
                var ddl = document.getElementById("dropdown");
                var text = ddl.options[ddl.selectedIndex].innerHTML;
                txtInput.style.color = text;
           
        }
   
<select id="dropdown" onchange="changeColor()"> 
        <option id="disabledselected">color change</option>
        <option id="btnTogglered">red</option>
        <option id="btnToggleblue">blue</option>
        <option id="btnTogglegreen">green</option>
    </select>
    
     <input type="text" name="txtInput" id="txtInput" value="TEXT INPUT">

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

PHP-based user interface queue system

I am in the process of developing a website that enables users to manipulate a webcam by moving it from left to right. Each user will have a one-minute window to control the camera. I plan on implementing a queuing system on the site to ensure that users ...

Why isn't Gzip compression working in webpack? What am I missing?

After comparing the compression results of manual webpack configuration and create-react-app for the same application, it became clear that create-react-app utilizes gzip compression, resulting in a significantly smaller final bundle size compared to manua ...

Bootstrap columns are still disrupted by large images even when they have reached their maximum width and height

I have been using bootstrap templates that allow users to create displays with images and text. I previously added max-width:100% and height:auto to the img CSS, thinking it would resolve issues with large images breaking the container. However, when inse ...

Encasing the Angular 2 component template in a <div> tag

Currently, I have a parent component managing multiple child components. My goal is to enclose each child component's template with a *ngIf directive for conditional rendering. The number of children in the parent component can vary. Here is an examp ...

Instructions for overlaying a text onto the select input field in DataTables

I am currently utilizing the DataTables select input feature to capture only the first three columns of data. However, I would like to enhance this by adding a text element above the select inputs within the DataTables interface. Is there a way to achieve ...

Adjusting the outline color of a Material UI Select component in outlined mode to a different color when the dropdown is activated (currently shown as blue)

Is there a way to change the outline color of Material-UI select component outlined variant correctly? I need help changing the default blue color to red, any assistance would be greatly appreciated Click here for an image reference Reference code snippe ...

Looking to adjust the alignment in BootsFaces? Want to move the final link of the navbar to the right corner?

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.c ...

Just a Quick Query About Regular Expressions

I need help removing a specific part from a URL string, which looks like this: http://.....?page=1. I am aware that the code "document.URL.replace("?page=[0-9]", "")" does not work, so I am curious to learn how to accomplish this task correctly. Thank you ...

Ember 2: Display a loading message only if the IDs were part of the initial response

I frequently use the following code snippet in my projects: {{#each model.posts as |post|}} <div>post.title</div> {{else}} <div>Loading the posts...</div> {{/each}} However, I sometimes face uncertainty regarding whether t ...

The error message that appeared states: "TypeError Object[object object] does not have the SubSelf method, TypeError Object[object object] does not

As I delved into a WebGL project, leveraging the powerful Sim.js and Three.js libraries, an unexpected obstacle emerged: At a certain point, within the code, the constructor for THREE.Ray is utilized in this manner: var ray = new THREE.Ray( this.camera.p ...

Tips for changing the background color depending on the value

I am creating a table displaying the results of a tournament. Each team's final placement and original seeding will be listed. I plan to include a small bubble next to each team showing how their final placement compares to their initial seeding. To v ...

difficulty with displaying the following image using jquery

I have referenced this site http://jsfiddle.net/8FMsH/1/ //html $(".rightArrow").on('click',function(){ imageClicked.closest('.images .os').next().find('img').trigger('click'); }); However, the code is not working ...

The functionality of the calculator, created using HTML and JavaScript, is impeded on certain devices

I developed a web-based app that functions as a simple calculator for calculating freight/shipping prices to Venezuela. The app allows users to select between 1 to 4 packages, and choose different types of freight including air (normal, express) and mariti ...

Utilizing deployJava.runApplet for precise element targeting

After years of successfully maintaining an applet that utilizes the traditional: <script src="foo.js"></script> method for embedding a Java applet, we can no longer ignore the need for change. It's time to switch to: deployJava.runAppl ...

What are some methods to prevent cookies from being overridden?

Just beginning my journey in web development. Currently utilizing asp.net Web API and Angular with token authentication. Every time a user logs in, I set the token in a cookie and send it with each request. Everything has been running smoothly so far, bu ...

Can the tab button be used to move to the next field?

Is it possible to navigate to the next field by pressing the tab button? If so, how can we achieve this? Currently, I am utilizing Bootstrap classes col-md-6. Thank you! <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4. ...

Troubleshooting: Issues with Mod_rewrite affecting CSS, JS, and Image functionality

Today marks my first time posting a query in this forum. I'm currently facing an issue with mod_rewrite. Below is the structure of my filesystem: site_new/stylesheets/layout.css site_new/javascript/somescript.js site_new/hallendetails.php ...

obtaining information from newly added form elements in an Angular application

I'm currently working on an app with the MEAN stack. I've managed to dynamically add form elements, but I'm running into an issue where all dynamically added elements are taking the same data when I use ng-model="something.something". What I ...

Issues arise when implementing a sticky position using CSS with incomplete functionality

I want to create a website with a menu bar that stays at the top even on long pages. However, when I use position: sticky in CSS, the menu disappears after scrolling a certain distance. Does anyone know how to fix this issue? HTML: <nav> <ul cla ...

Tips for creating a new tab or window for a text document

Below code demonstrates how to open a new tab and display an image with a .jpg extension. Similarly, I want to be able to open a text document (converted from base64 string) in a new tab if the extension is .txt. success: function(data) { var ...