To change the font color to red when clicked, I must create a button using HTML, CSS, and Javascript

Currently, I am utilizing CodePen to assess my skills in creating a website. Specifically, I am focusing on the HTML component. My goal is to change the font color to blue for the phrase "Today is a beautiful sunny day!"

Here is the snippet of code that I have developed so far:

<!DOCTYPE html>
<html>
  <head>
    <style>
      h1  {color: red; } 
      #redText {color: red } 
         </style>
     <title>your site</title> 
  </head>
   <body>
    <h1> Today's Weather Report</h1>
    <div class="redText">
    <p id="blueText"> Today is a beautiful sunny day!</p>
      
     <button type="button">Push</button> 
   
    <script>
     function changeColor(newColor){var elem = document.getElementById('blueText');
    elem.style.color = newColor;}
    <button onClick="changeColor(blue);">PUSH</button>
    </script>
   </body>
</html>

When looking at the current script I've written, it displays as follows:

Today's Weather Report (The font color should be red for this line and indeed it is.)

Today is a beautiful sunny day! (This sentence is expected to turn blue upon clicking the PUSH button, however, it remains black.)

At the bottom, you'll find a button labeled PUSH

Answer №1

avoid placing buttons within script tags :)

<!DOCTYPE html>
<html>
  <head>
    <style>
      h1  {color: red; } 
      #redText {color: red } 
         </style>
     <title>your site</title> 
  </head>
   <title>My Name Here</title> 
   </head> 
    
  <body>
    <h1> Today's Weather Report</h1>
    <div class="redText">
    <p id="blueText"> Today is a beautiful sunny day!</p>
    <button onClick="changeColor('blue');">PUSH</button>

   
    <script>
     function changeColor(newColor){
       var elem = document.getElementById('blueText');
       elem.style.color = newColor;
     }
    </script>
   </body>
</html>

Remember to enclose the color parameter in quotation marks since it's a string property!

Answer №2

To implement this change in the script, you need to take out the following code:

<button onClick="changeColor(blue);">PUSH</button>

Then, move the function call above into the html button tag.

<html>
  <head>
    <style>
      h1  {color: red; } 
      #redText {color: red } 
         </style>
     <title>your site</title> 
  </head>
   <title>My Name Here</title> 
   </head> 
    
  <body>
    <h1> Today's Weather Report</h1>
    <div class="redText">
    <p id="blueText"> Today is a beautiful sunny day!</p>
      
     <button type="button" onClick="changeColor('blue')">Push</button> 
   
    <script>
     function changeColor(newColor){
       var elem = document.getElementById('blueText');
       elem.style.color = newColor;
     }
    </script>
   </body>
</html>

Answer №3

It seems like the title is indicating a color change from blue to red, but there might be a missing word in there. Regardless, changing the text color can be easily accomplished in a few different ways as demonstrated below. Also, it looks like the DIV tag was not closed properly (<div class='redText'>), so I took it out of the snippet.

const clickhandler=function(e){
  let p=this.previousElementSibling;
  p.className=p.className=='blueText' ? 'redText' : 'blueText'
}
const togglehandler=function(e){
  this.previousElementSibling.classList.toggle('redText');
}

document.querySelector('button[name="bttn-1"]').addEventListener('click',clickhandler);
document.querySelector('button[name="bttn-2"]').addEventListener('click',togglehandler);
h1 { color:red } 
.redText { color:red!important; }
.blueText{ color:blue; }
<h1> Today's Weather Report</h1>

<p class="blueText"> Today is a beautiful sunny day!</p>
<button name='bttn-1' type="button">Push</button>

<p class="blueText">Today is also a beautiful hot and sunny day!</p>
<button name='bttn-2' type="button">Push-Toggle</button>

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

I am experiencing issues with the visibility of my background on certain mobile browsers

Apologies for repeating a similar question, but I've already tried the solutions mentioned in other posts. On my webpage, I have a table with a background image set using CSS. html { width:100% height: 100%; margin: 0px; padding: 0px; border: none; } ...

Having trouble getting JSON data to display in CanvasJS

I am trying to retrieve JSON data using Ajax with this code. It works fine when fetching data from the original source, but it's not working with my own JSON data. What could I be missing? Any help would be greatly appreciated. Thank you. $(document) ...

Avoid Refreshing the Page When Pressing the Like Button

I've been working on code for liking a post and saving the value to a database using server-side code. However, I'm running into some issues when the page refreshes after clicking the like button. I tried using event.preventDefault() in my JavaSc ...

Leveraging deferred for linking loops involving several ajax requests

Despite the fact that several questions have been answered on this topic, none of them seem to be effective in this particular scenario. function login(u,p) { console.log(1); return $.post(url, {u,p}); } function out() { console.log(3); //a f ...

Maintain the style of the main navigation menu when hovering over the sub-navigation items

I am currently in the process of redesigning the navigation bar for a specific website. The site utilizes Bootstrap, with a main navbar and a secondary navbar-subnav. While I have been able to style both navs accordingly, I am encountering difficulties whe ...

The sequence of HTML attributes

Could there be a subjective angle to this question (or maybe not)... I work on crafting web designs and applications using Visual Studio and typically Bootstrap. When I drag and drop a CSS file into an HTML document, the code generated by Visual Studio loo ...

How come (23 == true) is incorrect but (!!23 == true) is correct? After all, there is === for exact comparisons

The question boils down to this: are both 23 and true truthy values? If so, shouldn't they be equal under the loose comparison operator ==? However, there is also the strict comparison operator === for cases where precise equality is required. UPDATE ...

Sending a Boolean value from a child component to its parent state in React JS

Within my application, I have implemented a login feature in the navbar component. However, I am encountering an issue with updating a variable in the main component upon successful login. Although I have successfully managed to set up the login functional ...

Choose the current parent item using Angular's ng-selected and $index

http://plnkr.co/edit/fwwAd4bn6z2vxVN2FUL7?p=preview On the Plunker page linked above, I am trying to achieve a specific functionality. I would like the 3 dropdown lists to display values A, B, and C initially. When a 4th dropdown option is added, it shoul ...

Exploring the features of useEffect and setState in hook functions

Exploring the idea of utilizing React to efficiently fetch data using useEffect correctly. Currently facing a situation where data fetching is occurring constantly instead of just once and updating only when there is an input for the date period (triggeri ...

Learn how to efficiently process a data queue with AngularJS using Promises

Within my AngularJS application, I have a Service and multiple controllers running simultaneously. The app receives data updates from the server in JSON format through the Angular Service. To manage the vast amount of data, I need to queue it within the se ...

Concatenate a variable to the conclusion of a string using PHP

Currently, I am trying to include the id of the logged-in user in a table name. For instance, I would like to have a table named Rating_User_1 where 1 corresponds to the id of the user who is currently logged in. I have stored the user's id in a varia ...

What benefits does React offer that jQuery does not already provide?

What sets ReactJS apart from jQuery? If jQuery can already handle everything, why should we use React? I've tried to research on Google but still don't have a clear answer. Most explanations focus on terms like "views," "components," and "state" ...

Vibrant Reverberation of Color

When creating a table connected to a MySQL database, I encountered an issue with changing the text color inside the rows. How can I display the text in white within the table? I attempted to use <style = color #34242, but it didn't produce the des ...

How can we seamlessly transition from adding a class to setting scrollTop on a particular div?

I have successfully implemented a jQuery script to change the background color of my navbar on scroll. Here's the code I used: jQuery(document).ready(function($) { $(window).scroll(function() { var scrollPos = $(window).scrollTop(), nav ...

Choosing a specific category to display in a list format with a load more button for easier navigation

Things were supposed to be straightforward, but unexpected behaviors are popping up all over the place! I have a structured list like this XHTML <ul class = "query-list"> <li class="query"> something </li> <li class="query" ...

Unable to save cookies on mobile browsers, but functioning properly on desktop computers

Currently, I am facing an issue with my ExpressJS app hosted on Heroku and a frontend React app hosted on Netlify. The problem arises when a user logs in successfully and is directed to the home page showing personalized content. Upon landing on the home p ...

What could be causing React onclick events to not trigger when wrapped within a Vue application? (No additional libraries)

As I dive into the world of combining React and Vue components, I encountered an interesting challenge... const RootTemplate = () => { return ( <div id="vue-app"> ... <IconButton color="inherit" onClick={thi ...

Guide on altering the background color of a table row depending on the data in its cells with the help of AngularJS

I am looking to dynamically change the background color of a row based on specific cell data. If the first four characters in a table cell match a certain value, I want the entire row to change its color to red. Currently, my code changes the row color ba ...

Tips for configuring identical libraries under different names

As a Japanese web developer, I struggle with my English skills, so please bear with me. Currently, I am utilizing an npm library. I have forked the library and made some modifications to it. In order to incorporate these changes, I updated my package.js ...