Call Javascript function from HTML

Call Javascript function from HTML can be done in following ways

  1. Call on button click
  2. Call on form submission
  3. Call on onload event
  4. JavaScript function call with parameters

Call javascript function from HTML button click

Here in HTML, a button is created on the button’s click event a method hello() is called.

Fig: Alert function call on button click
Fig: Alert function call on button click

We have created input button which will show click me as specified in value. On click on button it will call hello().

We defined hello function in script tag.

To defined hello() we used function hello() and opening and closing {}.

Inside parenthesis we will write work of hello function.

Here we have defined only a alert box which will print Hello Friends.

Above examples shows how we mapped a button with JavaScript function.

Call on form submission

We are calling a method check() on form submission.

Whenever user click on submit button the method check() is get called.

We have a form tag inside form there are label and text box for User name and label and Password box and a submit button.

On click on submit form will call submit() JavaScript method.

Inside check function it will generate a alert box and show checking your form and come back to form.

HTML on onload event

Onload, event can be used to call a function when the page is loaded.

to call a function on onload event we used <body onload="fun1()">.

When HTML document will load it will call JavaScript function fun1().

Html call javascript function with parameters

Call javascript function from HTML button click Getting the value using getElementById() in function

Read More

Datatype in JavaScript

Javascript jquery checkbox checked, unchecked, ischecked