How to Change Element HTML Properties on Click?

10 minutes read

To change an HTML element's properties on click, you can use JavaScript. Below is an example of how you can achieve this without using a list:


First, you need to have an HTML element that you want to modify. For this example, let's assume you have a button element with an id "myButton":

1
<button id="myButton">Click me</button>


To change the HTML properties of this element when it is clicked, you can add an event listener using JavaScript:

1
2
3
4
5
6
7
8
9
// Get a reference to the button element
var button = document.getElementById("myButton");

// Add a click event listener to the button
button.addEventListener("click", function() {
  // Change the HTML properties of the button element
  button.innerHTML = "Clicked!";
  button.style.backgroundColor = "red";
});


In the above code, we first fetch the button element using getElementById and store it in a variable called button. Next, we add a click event listener to the button using the addEventListener method.


Within the event listener function, you can change the HTML properties of the button element. In this example, we change the innerHTML to display "Clicked!" and the background color to red.


This is just a basic example, and you can modify the element's properties to suit your specific needs.

Best HTML & CSS Books to Read in 2024

1
Web Design with HTML, CSS, JavaScript and jQuery Set

Rating is 5 out of 5

Web Design with HTML, CSS, JavaScript and jQuery Set

2
HTML and CSS QuickStart Guide: The Simplified Beginners Guide to Developing a Strong Coding Foundation, Building Responsive Websites, and Mastering ... Web Design (QuickStart Guides™ - Technology)

Rating is 4.9 out of 5

HTML and CSS QuickStart Guide: The Simplified Beginners Guide to Developing a Strong Coding Foundation, Building Responsive Websites, and Mastering ... Web Design (QuickStart Guides™ - Technology)

3
HTML, CSS, and JavaScript All in One: Covering HTML5, CSS3, and ES6, Sams Teach Yourself

Rating is 4.8 out of 5

HTML, CSS, and JavaScript All in One: Covering HTML5, CSS3, and ES6, Sams Teach Yourself

4
Head First HTML and CSS: A Learner's Guide to Creating Standards-Based Web Pages

Rating is 4.7 out of 5

Head First HTML and CSS: A Learner's Guide to Creating Standards-Based Web Pages

5
HTML, CSS & JavaScript in easy steps

Rating is 4.6 out of 5

HTML, CSS & JavaScript in easy steps

6
HTML and CSS: Visual QuickStart Guide

Rating is 4.5 out of 5

HTML and CSS: Visual QuickStart Guide

7
HTML & CSS: The Complete Reference, Fifth Edition (Complete Reference Series)

Rating is 4.4 out of 5

HTML & CSS: The Complete Reference, Fifth Edition (Complete Reference Series)

8
Beginning HTML and CSS

Rating is 4.3 out of 5

Beginning HTML and CSS

9
HTML, XHTML and CSS For Dummies

Rating is 4.2 out of 5

HTML, XHTML and CSS For Dummies

10
HTML & CSS: The Good Parts: Better Ways to Build Websites That Work (Animal Guide)

Rating is 4.1 out of 5

HTML & CSS: The Good Parts: Better Ways to Build Websites That Work (Animal Guide)


How to change the text color of a group of elements on click?

To change the text color of a group of elements on click, you can use JavaScript and CSS. Here's an example:


HTML:

1
2
3
4
5
6
7
<div class="element-group">
  <p>Element 1</p>
  <p>Element 2</p>
  <p>Element 3</p>
</div>

<button onclick="changeTextColor()">Change Color</button>


CSS:

1
2
3
.pink-color {
  color: pink;
}


JavaScript:

1
2
3
4
5
6
7
function changeTextColor() {
  var elements = document.querySelectorAll('.element-group p');

  for (var i = 0; i < elements.length; i++) {
    elements[i].classList.toggle('pink-color');
  }
}


In this example, we have a group of elements inside a div with the class "element-group". We also have a button that triggers the "changeTextColor" function when clicked.


The JavaScript function selects all the paragraphs (p elements) inside the div using the querySelectorAll method. Then, it iterates through each element and toggles the class "pink-color" using the classList.toggle method.


Finally, we defined a CSS class called "pink-color" with a color of pink, which will be applied to the elements when the button is clicked.


How to change the visibility of an element on click?

To change the visibility of an element on click, you can use JavaScript and CSS. Here's an example of how you can do it:


HTML:

1
2
<button id="toggleButton">Toggle Element</button>
<div id="elementToToggle">This element will be toggled</div>


CSS:

1
2
3
#elementToToggle {
  display: none; /* Hide the element by default */
}


JavaScript:

1
2
3
4
5
6
7
8
document.getElementById('toggleButton').addEventListener('click', function() {
    var element = document.getElementById('elementToToggle');
    if (element.style.display === 'none') {
        element.style.display = 'block'; // Show the element
    } else {
        element.style.display = 'none'; // Hide the element
    }
});


In this example, when the button with the toggleButton id is clicked, the JavaScript code inside the event listener will be executed. It checks the current display property of the elementToToggle element and toggles it between block (to show the element) and none (to hide the element) accordingly. The CSS display property is used to control the visibility of the element.


How to change the border color of an element on click?

To change the border color of an element on click, you can use JavaScript to modify the CSS style of the element.


Here is an example of how you can achieve this:


HTML:

1
<div id="myElement"></div>


CSS:

1
2
3
4
5
6
#myElement {
  width: 200px;
  height: 200px;
  border: 2px solid black;
  cursor: pointer;
}


JavaScript:

1
2
3
4
5
6
7
8
// Get the element
var element = document.getElementById("myElement");

// Add a click event listener to the element
element.addEventListener("click", function() {
  // Change the border color of the element
  element.style.borderColor = "red";
});


In this example, the element with the id "myElement" has a default border color of black. When it is clicked, a click event listener triggers a function that changes the border color of the element to red by modifying its style.borderColor property.


How to change the border width of an element on click?

You can change the border width of an element on click using JavaScript. Here's an example:


HTML:

1
<button id="myButton">Click me</button>


CSS:

1
2
3
4
5
6
7
.myElement {
  border: 2px solid black;
}

.myElement.highlight {
  border-width: 4px;
}


JavaScript:

1
2
3
4
5
6
7
8
// Get the element
var myElement = document.getElementById("myButton");

// Add click event listener
myElement.addEventListener("click", function() {
  // Toggle the "highlight" class
  this.classList.toggle("highlight");
});


In the example above, when the button is clicked, it will add or remove the "highlight" class to the element. The "highlight" class is defined in CSS with a different border-width, causing the border width of the element to change on click.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To hide an element using CSS, you can use the display property. Here are a few ways to do it:display: none;: This property completely hides the element from the page layout. It will be as if the element doesn&#39;t exist. It also removes the space that the ele...
To make a &lt;div&gt; element clickable in HTML and CSS, you can use the following steps:Step 1: HTML Markup &lt;div id=&#34;clickable-div&#34;&gt;&lt;/div&gt; Step 2: CSS Styling You can provide a CSS selector to target the &lt;div&gt; element using its id or...
To animate HTML canvas curve lines, you can use JavaScript and the canvas element. Here is an overview of the steps involved:Create a canvas element in your HTML markup: In your JavaScript code, retrieve the canvas element and get its 2D rendering context: con...