Adjust the text color when you select an element

I am trying to figure out how to change the color of my "footertext" to red when I click on either the "left" or "center" containers. Then, I want it to go back to white when I click on any of the containers (left, right, center).

This is my current HTML:

<p class="footertext" style="color:red">Designed by Clarke Cribb<br></p>


    <div class="container" id= "left" >
        <h1 style="color:white"><a>HAIR</a></h1>
    </div>

    <div class= "container" id= "center">
        <h1 style="color:white"><a>BEAUTY<a/></h1>
    </div>

    <div class="container" id= "right">
        <h1 style="color:white"><a>BARBERS</a></h1>
    </div>
</div>

Here is my JavaScript:

<script>
    $(document).ready(function(){
        $("#left, #center").click(function(){
            $("footertext").css("color","red");
        });
        
        $(".container").click(function(){
            $("footertext").css("color","white");
        });
    });
</script>

If anyone has a solution, I would greatly appreciate it. Thank you! - Clarke

Answer №1

Check out this code snippet:

 <script>

   $(document).ready(function(){
 $("#left,#center").click(function(){ 
  if($(".footertext").css("color") == 'rgb(255, 0, 0)')
  {
    $(".footertext").css("color","white");
  }
  else
  {
    $(".footertext").css("color","red");
  }
});
$("#right").click(function(){
  $(".footertext").css("color","white");
});
 });
</script>

Give it a try and see how it works for you!

Answer №2

You have the ability to switch between classes.

Implement the following CSS code:

.blue {
    color: blue;
}

Adjust your javascript to this:

$(function() {
    $("#up,#down,#left").click(function(){
        $(this).toggleClass('blue');
    });
});

Finally, eliminate any inline styles from your HTML elements.

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

AngularJS implemented a header authorization feature

I've been attempting to include an authorization header in my requests, but I'm facing some issues. Here is the code I am using: var config = {headers: { 'Authorization': token } }; return $http.get('http://localhost:3000/api ...

Display the image regardless of whether the component is currently visible

I need help with my Vue.js web application that includes a side navigation menu component. This component uses conditional rendering to display only when necessary. Within the component, there is an image for the close button of the side menu. <transiti ...

What is the process by which HTML strings are being attached to an output variable and how does the join function affect the map method?

I am encountering some difficulty grasping the significance of this section of code. I have included a large portion of the code to provide context and aid in understanding what the original author intended with this code snippet. Within the replaceTempla ...

How to efficiently filter objects within a child array in an ng-repeat statement, utilizing track by $index to manage duplicates

I have a variable called narrow.searchedMenu that contains 3 arrays of equal length. To display them, I am using ng-repeat with track by $index due to some duplicate elements present. <ul> <li ng-repeat="item in narrow.searchedMenu.desc ...

Error encountered during Atom execution - The command '/usr/bin/env: 'node' was not found in the directory

Just starting out with coding on Atom and I'm stuck dealing with the same error message every time I try to run my Javascript code. bash: line 1: node: command not found /usr/bin/env: ‘node’: No such file or directory I've searched for solu ...

Encountering an error: "Unhandled promise rejection SyntaxError: Unexpected character in JSON data at line 1 column 1."

When the submit button is clicked, my registration form data is sent using an event function called postData() in React. The user data is sent at the register route, and I have mentioned line numbers in the comments: const postData = async(event) =>{ ...

Paper.js - generate a collection of movable shapes

I am attempting to use paper.js to create draggable shapes where the index of each shape is provided during dragging. Unfortunately, I keep encountering an error that says "Uncaught TypeError: Cannot read property 'position' of undefined". If a ...

Guide to positioning a border with CSS

How can I align a border to the right in this code? This is the CSS snippet that I currently have: p { text-align: right; color: #757575; Background-color: #FFFFFF; font-size: 2.4em; width: 50px; } <p>Home<br> <a href="aboutu ...

Unable to extract attributes from a different model within Sails.js

I'm working on populating a customer model with attributes from the address.js model. However, when trying to post JSON using Postman, I keep getting a 500 Validation Error and struggling to pinpoint the cause of the issue. Any assistance would be gre ...

Styling with CSS and strategic placement

I'm having an issue with my form elements - specifically a text input and a submit button. I want them to display inline, but the submit button is appearing slightly above the input field. I tried placing both elements in a div and setting their maxim ...

Validating groups of fields using Angular fieldsets

AngularJS validation is working well with ng-required. However, I am interested in checking if all the form elements within my fieldset are valid. <form> <fieldset> <legend> Part one <img src="/co ...

Checkbox Event Restricts Access to a Single Class Variable

As a beginner in AngularJS (just diving in this week), I've encountered an issue with a checkbox input connected to an ng-change event. <input type="checkbox" ng-model="vm.hasCondoKey" ng-change="vm.onKeyCheckboxChange()" /> The ng-change even ...

Ensure that the floating div is positioned along the right side and adjusts to accommodate the

I've been searching for a solution to this issue, but it seems like I might not be articulating it correctly because I haven't been able to find an answer yet. On my page, there is a 'quick links' button that is supposed to stay fixed ...

Issue with button click functionality within Angular grid's Celltemplate

Has anyone experienced issues with the click event not working when using celltemplate in AngularJS? $scope.format = function(val){ return val.replace(/\//g, ""); }; var executionColumns = { data: [], ...

Structure of directories in NodeJS

I'm diving into the world of web development and NodeJS, embarking on the task of streamlining our app's code and directory layout. Seeking the insights of seasoned developers, I am keen to gather opinions on the file structure pattern I am cons ...

Is there a way to transform a Base64 image into a specific file type?

Is it possible to integrate my website with an iOS device and utilize the device's camera functionality? After capturing an image, the camera returns it in a base64 image format. Is there a way to convert this to a file type? If the mobile device pr ...

Issue encountered when utilizing ngResource in factory: unable to choose JSON index

ngResource in a factory is functioning properly, but unfortunately, it is only able to select the index of the JSON. However, it is also possible to bind the same variable $scope.resultItems. The console log appears as follows ...

Trouble with Establishing Fresh Routes in Express.js

I am currently using express.js to develop an application with two ejs pages that are accessible through navigation. However, I am looking to expand the functionality of my app by incorporating more ejs (pug) pages to make it more dynamic. I want to update ...

The HTML code starts with a fluid middle column in a 3-column layout, while the other two columns have fixed widths

I am interested in creating a traditional 3-column layout similar to this: | | | | |L | M |R | | | | | I have been tasked with using the following html structure: as depicted, the Main div is the initial node of #container <body> ...

Deselect the checkbox if you are checking a different checkbox

Struggling with my code, it's hit or miss. Feeling defeated. Seeking assistance to uncheck a checkbox when another checkbox is checked. Check out the code below: JavaScript: $("#1 #checkAll").change(function() { if ($("#1 #checkAll").is(' ...