Functions are blocks of reusable code that can perform a specific task. In web development, functions are commonly used to manipulate or interact with HTML elements.

In HTML, you can use JavaScript to define and call functions. Here’s an example of HTML code that contains a function:

“`html

function changeColor() {
var element = document.getElementById(“myElement”);
element.style.color = “red”;
}

Change Color

This text will change color when the button is clicked.

“`

In this example, we have an HTML `button` element with an `onclick` attribute that calls the `changeColor` function when clicked. The `changeColor` function uses the `getElementById` method to get the HTML element with the id “myElement” and changes its `style.color` property to “red”.

When the button is clicked, the `changeColor` function will be executed, and the text inside the `p` element with the id “myElement” will change color to red.

Design a site like this with WordPress.com
Get started