how to call javascript function in html?

Today, We want to share with you how to call javascript function in html?.In this post we will show you onclick javascript, hear for calling external javascript function from html we will give you demo and example for implement.In this post, we will learn about call javascript function from html with an example.

how to call a function inside a function in javascript?

There are the Following The simple About call javascript function from html without event Full Information With Example and source code.

As I will cover this Post with live Working example to develop how to call a function in javascript, so the javascript call function by name is used for this example is following below.

Read Also:  how to call javascript function in html & JavaScript Define

Call a javascript function via html attribute

<button type='button' data-button='{"option1": "option1", "option2": "option1"}' data-button-function="testFunction()">click1</button>

JS CODE

  
$('button').click(function(){
  var data = $(this).attr('data-button-function');
  //alert(data.name);
    eval(data);
});

function testFunction(){
    alert("test ok")
}
 

Calling a Javascript Function with an Alert & Confirm Button in HTML

<h1>Change Things I</h1>
<div id="d1">
  Hello 
</div>
<div id="d2">
  Goodbye
</div>
<p>
  <input type="button" value="fucking click the button" onclick="dochange()">
</p>
<p>
  <input type="button" value="fucking confirm button" onclick="confirmIt()">
</p>

JS CODE

function dochange(){
  alert('You just clicked the fucking button with JS function');
}

function confirmIt(){
  var r = confirm("You just clicked the fucking confirm box");
  if (r == true){
    alert('You just fucking pressed OK');
  } else {
    alert('Are you sure you want to fucking cancel');
  }
}

CSS CODE

h1 {
  background-color:
    green;
}
div {
  width: 200px;
  font-size: 28px;
}
#d1{
  background-color:
    lightblue;
}
#d2{
  background-color: yellow;
}

jQuery on click call function with parameters

<div class="container">
  <h1>Click the buttons</h1>
  <div class="row">
    <div class="col-sm-6">
      <button data-div-id="1" class="button">
         Show Div 1
      </button>
      <button data-div-id="2" class="button">
        Show Div 2
      </button>
    </div>
    <div class="col-sm-6">
      <div id="div1" class="_hidden">Showing Div 1</div>
      <div id="div2" class="_hidden">Showing Div2</div>
    </div>
  </div>
</div>

JS CODE

$(document).ready(function() {
  $('.button').on('click', function() {
    var div_id = $(this).attr('data-div-id');
    $('#div'+div_id).css('visibility', 'visible');
  });
});

CSS CODE

/* DO NOT COPY*/
* { text-align: center;}
.col-sm-6 { border: 1px dashed #21ba45;}
.container { margin-top: 25px;}
._hidden {
  visibility: hidden;
  background-color: #21ba45;
  color: #ffffff;
  width: 100%;
  margin: 5px;
}
/* END DO NOT COPY*/

Web Programming Tutorials Example with Demo

Read :

Read Also:  jquery get hidden field value

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about how to call javascript function in html.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.