All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript To Click A Button

Last Updated : Mar 11, 2024

JavaScript To Click A Button

In this article we will show you the solution of JavaScript to click a button, we will use the JavaScript function to click on a button. The syntax of functions in JavaScript is,

function name(parameter1, parameter2,......) {
// block of code
}

We will also set the background color by clicking on a button in JavaScript. The syntax is,

Element.style.CSSproperty = value ;

Step By Step Guide On JavaScript To Click A Button :-

Example 1

Let us see the code for the simplest example of JavaScript by clicking on a button.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript to click a button</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        body{
            background-color: aliceblue;
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> JavaScript to click a button </h3>
    </center>
    <input type="button" value="click" onclick="example()">
    <script>
        function example() {
            document.write("WELCOME TO TALKERSCODE") ;
        }
    </script>
</body>
</html>
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with the </head> tag.
  4. The HTML document's <title> is then established using the title tag, which is followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. using <style> tag to add CSS
  7. create a <Center> tag the add a <h1> tag used to add a heading close it with </h1> tag and add a <h3> tag within it
  8. create an <input> tag with type of button and value of click. With a onclick function of example()
  9. To write JavaScript, create a <script> tag
  10. Now declare the function example and write a sentence inside of document.write()
  11. Lastly by clicking on the button , the text will be display on the HTML page

Example 2

In this example, we will change the background color of a HTML page by clicking on a button using JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript to click a button</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        body{
            background-color: aliceblue;
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> JavaScript to click a button </h3>
    </center>
    <input type="button" value="change background-color" onclick="changeBackground()">
    <script>
                function changeBackground() {
            document.body.style.background = "pink";
        }
    </script>
</body>
</html>
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with the </head> tag.
  4. The HTML document's <title> is then established using the title tag, which is followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. using <style> tag to add CSS
  7. create a <Center> tag the add a <h1> tag used to add a heading close it with </h1> tag and add a <h3> tag within it
  8. Create an <input> tag with the type of button and the value of changing the background color. With a onclick function
  9. To write JavaScript, create a <script> tag
  10. At first declare the function for changing the background color.
  11. Write Document.body.style.background and add a color
  12. At last, by clicking on the button, we can change the background color.

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know How to click a button using JavaScript.

I hope this article on JavaScript to click a button helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪