Last Updated : Jul 1, 2023
Today, in this article we will talk about how to change background color of div by click on button. Let us see the code to change background color.
As, there are many ways with help of which we can apply onclick method to change background color.
To understand this article let us first write some content using heading, div and create a button on which we click to change color.
Let’s see the codes and understand with help of steps.
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Here, inside body we create some headings, paragraphs and a button also. Paragraphs are too show data and on button click the background color gets changed.
- Inside script, we show text of first paragraph which have id output1.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.