How to Create Button in Javascript
In this tutorial, you will learn how to create button in javascript. Dynamically creating a button
element in javascript is not straightforward. For newbie developers, it can be a bit tricky since there is no built-in method just to create a button.
Having said that, we do have a document.createElement()
method to create an element. Using this method, we can create any element of our choice. There is another method document.createTextNode()
and that will create a node for text content. We can append this node as a child to the element returned by document.createElement()
method to have some text content.
In the following example, we have a global array fruits
and that includes a bunch of fruit names. We have one input field in which we will type those fruit names one by one. As soon as we are done typing a fruit name that is part of the array, we will dynamically create a submit button. Please have a look over the code example and steps given below.
HTML & CSS
- We have 2 elements in the HTML file (
div
andinput
). Thediv
element with a class ofcontainer
is just a wrapper for theinput
element. - We have done some basic styling using CSS and added the link to our
style.css
stylesheet inside thehead
element. - We have also included our javascript file
script.js
with ascript
tag at the bottom.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div class="container"> <input type="text" placeholder="Enter Fruit Name"> </div> <script src="script.js"></script> </body> </html>
.container { display: flex; justify-content: space-between; align-items: center; flex-direction: column; height: 70px; } input { height: 20px; } button { min-width: 120px; }
Javascript
- We have selected the
input
element and thediv
element using thedocument.querySelector()
method and stored them in theinput
andcontainer
variables respectively. - We have a global variable
fruits
and it holds an array of fruit names. - We have attached
keyup
event listener to theinput
element. - In the event handler function, we are selecting the
button
element usingdocument.querySelector()
method and storing it in thebtnSubmit
variable. Since we do not have any button in our HTML file yet,document.querySelector()
method will return null. - If there is a
button
element, we would like to remove it first and for that, we are using theremoveChild()
method of thediv
element. - We are checking if the text entered in the
input
element is part of thefruits
array. I am getting the value of theinput
element from the event object usinge.target.value
, but you can also useinput.value
since we have already selected theinput
element in the beginning. - If text entered is part of the
fruits
array, then we will create abutton
element usingdocument.createElement()
method and store it in the local variablebtnSubmit
. - We will create a text node using
document.createTextNode()
method and store it in thebtnText
variable. - We will append the text node to the
button
element using theappendChild()
method. - We have to display this
button
element on the screen, so we have to append it to any of the existing elements. We are appending it to thediv
element using theappendChild()
method.
let input = document.querySelector('input'); let container = document.querySelector('.container'); let fruits = ['Apple', 'Orange', 'Kiwi', 'Grapes', 'Mango']; input.addEventListener('keyup', (e) =>{ let btnSubmit = document.querySelector('button'); if(btnSubmit){ container.removeChild(btnSubmit); } if(fruits.includes(e.target.value)){ let btnSubmit = document.createElement('button'); let btnText = document.createTextNode('Submit'); btnSubmit.appendChild(btnText); container.appendChild(btnSubmit); } });