Day 8: Create a Button in JavaScript | 10 Days Of JavaScript

Hello there, today we are going to solve Day 8: Create a Button Hacker Rank Solution in JavaScript which is a Part of 10 Days Of JavaScript Series.

Hello there, today we are going to solve Day 8: Create a Button Hacker Rank Solution in JavaScript which is a Part of 10 Days Of JavaScript Series.

Day 8: Create a Button in JavaScript
Table Of Contents 👊

Objective

In this challenge, we practice creating buttons in JavaScript.

Task

Complete the code in the editor so that it creates a clickable button satisfying the following properties:

  • The button's id is btn.
  • The button's initial text label is . After each click, the button must increment by . Recall that the button's text label is the JS object's innerHTML property.
  • The button has the following style properties:

A width of 96px.
A height of 48px.
The font-size attribute is 24px.

The .js and .css files are in different directories, so use the link tag to provide the CSS file path and the script tag to provide the JS file path:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="css/button.css" type="text/css">
    </head>
    
    <body>
    	<script src="js/button.js" type="text/javascript"></script>
    </body>
</html>

Submissions

This is a new style of challenge involving Front-End rendering. It may take up to 10 seconds to see the result of your code, so please be patient after clicking Submit. The Submissions page contains screenshots to help you gauge how well you did.

Explanation

Initially, the button looks like this: 0
After the first 4 clicks, it looks like this: 4
After 5 more clicks, it looks like this: 9

Solution - Day 8: Create a Button

index.html

<!-- Enter your HTML code here -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel ="stylesheet" href = "css/button.css" type ="text/css">
        <title>Button</title>
    </head>
    <body>
        <button class = "button" id = "btn">0</button>
        <script src = "js/button.js" type= "text/javascript"></script>
    </body>
</html>

button.css

.button {
    width: 96px;
    height: 48px;
    font-size: 24px;
}

button.js

var btn = document.getElementById("btn");
    btn.addEventListener("click", function(){
        var current_value = this.innerHTML;
        this.innerHTML  = parseInt(current_value)+1;
    });

Disclaimer: The above Problem (Create a Button) is generated by Hacker Rank but the Solutions is Provided by Sloth Coders. This tutorial is only for Educational and Learning Purpose.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment