Call Javascript Function On HTML Button Click




Here we consider how to call javascript function on button click. Lets start with one example here. Suppose we have a button inside div as shown below.

Call Javascript Function On HTML Button Click, Call Javascript Function On Button Click, Trigger Javascript Function On HTML Button Click, Invoke Javascript Function On HTML Button Click, Trigger Javascript Function On Button Click, Invoke Javascript Function On Button Click, Button Click JavaScript Function, Attach Javascript Function On Button Click, Javascript Function On HTML Button Click, Javascript Function On Button Click, JavaScript, HTML, PHP

Inside Srcipt region, we have declared a javascript function.

function clickEvent() {
    alert('hello!');
};



Now for calling this function on button click, we have two ways.




In this way, we make the OnClick event inline to the HTML part.

Call Javascript Function On HTML Button Click, Call Javascript Function On Button Click, Trigger Javascript Function On HTML Button Click, Invoke Javascript Function On HTML Button Click, Trigger Javascript Function On Button Click, Invoke Javascript Function On Button Click, Button Click JavaScript Function, Attach Javascript Function On Button Click, Javascript Function On HTML Button Click, Javascript Function On Button Click, JavaScript, HTML, PHP

Inside HTML button, write, onclick="clickEvent();".
Now when button is clicked, the JavaScript function will be invoked.




Here, we can attach the js function with the HTML button inside Script region. Inside Script region, find the HTML button by its ID and attach JS function.

document.getElementById("btnClick").onclick = clickEvent;



Now when button is clicked, the JavaScript function will be invoked. That's all, by this way we can Call Javascript Function On HTML Button Click.