PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 31, 2022

[FIXED] how to change the background-Color on click of a button in javascript

 October 31, 2022     css, html, performance     No comments   

Issue

I was able to change the color of the text but i am not able to understand how to change the background color of the p tag. If someone can please let me know hoe to do it. Below is my HTML and JS code.

function button(){
var btn = document.getElementById('btn');
btn.addEventListener('click', function onClick(event) {
  // Change text color globally
  document.body.style.color = 'red';
});
}    
button();
<style>
        p{
            box-sizing: border-box;
            border: 1px solid black;
            height: 20px;
            width: 300px;
            text-align: center;
        }
    </style>
    
<p>Welcome To My Domain</p>



    <button id="btn">Button</button>


Solution

You could add a class to the p tag and then query that within your event listener and set its style attribute, document.querySelector('.classname').style.backgroundColor = 'somecolor'

function button() {
  var btn = document.getElementById('btn');
  btn.addEventListener('click', function onClick(event) {
    // Change text color globally
    document.body.style.color = 'red';
    document.querySelector('.welcome').style.backgroundColor = 'black';
  });
}
button();
<style>
  p {
    box-sizing: border-box;
    border: 1px solid black;
    height: 20px;
    width: 300px;
    text-align: center;
  }
</style>

<p class="welcome">Welcome To My Domain</p>



<button id="btn">Button</button>

Another way would be to toggle a class that styles your element for you. With this example add the class and style it in your css and then use the classList in javascript to toggle the class. document.querySelector('.className').classList.toggle('someclass')

function button() {
  var btn = document.getElementById('btn');
  btn.addEventListener('click', function onClick(event) {
    // Change text color globally
    document.querySelector('.welcome').classList.toggle('instance-change')
  });
}
button();
.instance-change {
  color: red;
  background-color: black;
}

p {
  box-sizing: border-box;
  border: 1px solid black;
  height: 20px;
  width: 300px;
  text-align: center;
}
<p class="welcome">Welcome To My Domain</p>

<button id="btn">Button</button>



Answered By - dale landry
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing