What is the process for altering the color and text of the designated element using an if-else statement?

Issue: I would like to change the color of the first link to black and the remaining two links to gray when the continue button is clicked once. Then, on the second click of the continue button, I want the color of the second link to be changed to black while keeping the other links gray. This same sequence should repeat for the third click, with an additional change of the continue button text to "Save".

HTML:

<div class="nav2">
    <div class="navtxt container">
        <ol>
            <li id="navlnk">Assess your risk tolerance</li>
            <div id="b" class="fif"><a href="#navlnk1" onclick="changeColor('navlnk1'); return false;"><span class="glyphicon glyphicon-menu-right" style="padding-left: 20px;"></span></a></div>
            <li id="navlnk1">View your plan</li>
            <div id="b" class="fif"><a href="#navlnk2" onclick="changeColor('navlnk2'); return false;"><span class="glyphicon glyphicon-menu-right" style="padding-left: 20px;"></span></a></div>
            <li id="navlnk2">Open account</li>
        </ol>
    </div>
</div>
<input type='button' name='add' onclick="myFunction()"/>

JAVASCRIPT

function myFunction() {
document.getElementById("continue").value++; value='+'
if (value==1){
    document.getElementById('navlnk').style.color = "#000";
}
else if(value==2){
    document.getElementById('navlnk1').style.color = "#000";
}
else if(value==3)
{
    document.getElementById('navlnk2').style.color = "#000";
    document.getElementById('navlnk2').inner.html = "Save";

}

}

Answer №1

Check out this sample code that utilizes jQuery:

 <style>
        .black{
            color: black;
        }
        .hash{
            color:darkkhaki;
        }
    </style>
    <div class="nav2 container">
        <div class="navtxt container">
            <ol>
                <li id="navlnk"><a  href="#navlnk1"><span style="padding-left: 20px;" class="glyphicon glyphicon-ok-circle"></span>Assess your risk tolerance</a></li>
                <li id="navlnk1"><a href="#navlnk2"><span class="glyphicon glyphicon-chevron-right" style="padding-left: 20px;" ></span>View your plan</a></li>
                <li id="navlnk2"><a  href="#navlnk2" ><span class="glyphicon glyphicon-cog" style="padding-left: 20px;"></span>Open account</a></li>
            </ol>
        </div>
        <input type='button' name='add' class="btn" value="Continue" id="continue"/>
    </div>
    <script>
        $(document).ready(function () {
            var counter = 0;
            $('#continue').on("click", function () {
                var navLink = $('#navlnk a');
                var navLink1 = $('#navlnk1 a');
                var navLink2 = $('#navlnk2 a');
                if (counter == 0) {
                    $(navLink).addClass('black');
                    $(navLink2).addClass('hash');
                    $(navLink1).addClass('hash');
                }
                else if (counter == 1)
                {
                    alert('dsd');
                    $(navLink).removeClass('black').addClass('hash');
                    $(navLink1).removeClass('hash').addClass('black');
                    counter++;
                }
               else if(counter == 3) {
                   $(navLink1).removeClass('black').addClass('hash');
                   $(navLink2).removeClass('hash').addClass('black');
                    $(this).val('Save');
                }
                counter++;
            });
        });
    </script>

Answer №2

In order to achieve the desired outcome, you should create a function similar to the following one:

function adjustColor(selector) {
      if (selector === 'button1') {
               // Modify color for first button
               document.getElementById('button1').style.color = "#000";
        } else {
                // Modify color for second button
                document.getElementById('button2').style.color = "#000";
              }
   }

Hopefully, this solution meets your requirements.

Answer №3

I have made the necessary adjustments to your code based on your requirements. Please give it a try.

var num = 1;
function myFunction() {
var listArry = document.getElementsByClassName('list');
for(var i = 0; i < listArry.length; i++){
    listArry[i].style.color = "grey";
}
if (num==1){
document.getElementById('navlnk').style.color = "black";
num=2;
}
else if(num==2){
document.getElementById('navlnk1').style.color = "black";
    num=3;
}
else if(num==3)
{
document.getElementById('navlnk2').style.color = "black";
document.getElementById('btn').value = "Save";
num=1;
}
}
<div class="nav2">
    <div class="navtxt container">
        <ol>
            <li class="list" id="navlnk">Evaluate your risk tolerance</li>
            <div id="b" class="fif"><a href="#navlnk1" onclick="changeColor('navlnk1'); return false;"><span class="glyphicon glyphicon-menu-right" style="padding-left: 20px;"></span></a></div>
            <li class="list" id="navlnk1">Review your strategy</li>
            <div id="b" class="fif"><a href="#navlnk2" onclick="changeColor('navlnk2'); return false;"><span class="glyphicon glyphicon-menu-right" style="padding-left: 20px;"></span></a></div>
            <li class="list" id="navlnk2">Establish an account</li>
        </ol>
    </div>
</div>
<input id="btn" type='button' name='add' onclick="myFunction()"/>

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

Tips for Centering Text next to Logo on Mobile Display with Bootstrap 5

My website I've designed with Bootstrap 5 is facing responsiveness issues. While everything looks fine on desktop, the text and logo are misaligned in the mobile view. Here are screenshots demonstrating the problem: https://i.sstatic.net/jA5QTfFd.jp ...

Using JavaScript, locate the previous and next elements in the JSON array of objects based on the given current event ID

Task: To retrieve and display data based on the current event ID from a JSON file: var _getEventById = function(eventId) { var __findEvent = function(event) { return event.id === eventId; }; return _.find(data.meeting ...

Avoid running the second then() function in a promise if a specific condition is satisfied

Can anyone help me figure out how to prevent the code block below from executing any further once arkIsEnabled is evaluated as true? return promise .then(response => { if (arkIsEnabled) { dispatch(createArk(respo ...

Having difficulty implementing DragControls

My experience with three.js is at a beginner level, and I recently attempted to incorporate a feature allowing the dragging of a 3D model. During this process, I encountered DragControl but faced difficulty implementing it in my code. Upon using new DragCo ...

Unnecessary PHP code will run on its own in JavaScript

Attempting to initiate a session in PHP and assign a value to a session variable within a Javascript function on a PHP/HTML page is creating an issue. The problem arises when the PHP code inside the if statement runs automatically upon loading the page, ra ...

Issue encountered when attempting to establish a connection to mongoDB using Node.js: "The initial parameter supplied to mongoose.connect() must be a string."

When I tried to run app.js, I encountered the following error: Error message: 'The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string.' Er ...

Unable to submit form with Jquery

Having some trouble with my form submission using Jquery. The submit part of my code seems to be malfunctioning, and I can't pinpoint the issue. <?php if(!isset($_SESSION["useridentity"])){ die(header("Location:index.php")); } ...

What is the best way to align an object's rotation with a Vive controller's rotation when the trigger is pulled in A-Frame?

Exploring the possibilities of creating A-Frame applications, I decided to dive into the world of A-Frame Dominoes. In my current project, objects are spawned in response to the Vive trigger being pressed. While I have successfully managed to align the po ...

Positioning Problems with Popups

I have been facing an issue with the positioning of a popup in my trivia game. Despite trying multiple solutions, I have not been able to achieve consistency. Here is a snippet of the HTML code: <div class="logo"> <img src="css/rio-40.png"/& ...

Steps for making a dynamic number animation

Is there a way to create dynamic number changes when the user stops on a page using HTML, CSS, and JavaScript? I'd like the numbers to change as the user scrolls through the page until they come to a stop. I want the numbers to update quickly while t ...

Tips for solving issues with dependencies in React applications

After running "npm install," I encountered the following errors in the console: elena@elena-dev:~/PROYECTO FINAL CTD/grupo-12/frontend/proyecto-integrador$ npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While reso ...

Formatting numbers

I have a set of numerical values in my database, shown below: 1000 1500 10000 12500 100000 890000 1000000 6900000 My objective is to format these numbers for display on the frontend as follows: 1 000 1 500 10 000 12 500 100 000 890 000 1 000 000 6 900 0 ...

Ways to verify if any items in an array are present in a different array

I have two arrays of objects and need to determine if certain items are contained in another array. If any of them are not found, the result should be false. const arr = [ {full_name: 'Test'}, {full_name: 'Test1&apo ...

The font size of the <input> element is constantly fluctuating, causing

Basically, I am working on this code snippet for a calculator: #calculator { background-color: #212121; border-radius: 10px; overflow: hidden; display: inline-block; } #functionality { width: 100%; height: 100%; margin: 15px 5px 5px 15p ...

Can Next.js 13 components maximize performance by utilizing server and client components simultaneously? What is the best approach to achieve this?

Introduction Currently, I am exploring server and client components in Next.js 13 and looking to incorporate them into my project. One of the key features is a container component that utilizes react-intersection-observer to track which section is visible ...

What is the best way to update properties of an array object using a map in pure JavaScript for maximum efficiency?

Looking for an efficient solution to replace property keys in an array of objects using a map? Here's an example scenario: // Consider an array of objects: var records = [ {"Id": "0035w000036m j7oAAA", "Business Phone": ...

Cancellation of event by keypress handler

I found this code snippet: jQuery('#parent').on('keypress', '.textbox', function(e) { var btn = jQuery(this).closest('tr').find('.btn'); if (btn.length) { btn.triggerHandler('click&apo ...

Implementing conditional rendering with styled-components

Currently, I am working on a menu that consists of a list of MenuItem components: <MenuItem>A</MenuItem> <MenuItem>B</MenuItem> <MenuItem>C</MenuItem> I am looking to customize the componen ...

Viewing Image in Modal on Click

Despite numerous attempts, I have been unable to make this work, even after trying various examples. The issue at hand is with a list of imageURL's that are displayed on the webpage. The goal is to allow users to click on an image, which would then o ...

React - Component not updating after Axios call in separate file

Recently I decided to delve into React while working on some R&D projects. One of my goals was to build an application from scratch as a way to learn and practice with the framework. As I started working on my project, I encountered a rather perplexin ...