On click java script
- As in previous example, We have call a function in onclick HTML event also we can call that function for other purpose, but in this example we have created a function purticuallry for a HTML element.
- First button("btnid1") is based on function name, the function("fun_name") will call when the button clicked.
- In second button("btnid2") the function is directly call when click the button.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>JavaScript onclick</h1>
<input id="btnid1" type="button" value="Button 1">
<input id="btnid2" type="button" value="Button 2">
<script>
var btn1 = document.getElementById("btnid1");
var btn2 = document.getElementById("btnid2");
btn1.onclick = fun_name;
btn2.onclick = function()
{
alert("call function definition directly");
};
function fun_name()
{
alert("Function Called");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>JavaScript onclick</h1>
<input id="btnid1" type="button" value="Button 1">
<input id="btnid2" type="button" value="Button 2">
<script>
var btn1 = document.getElementById("btnid1");
var btn2 = document.getElementById("btnid2");
btn1.onclick = fun_name;
btn2.onclick = function()
{
alert("call function definition directly");
};
function fun_name()
{
alert("Function Called");
}
</script>
</body>
</html>
JavaScript:- On click JavaScript
Reviewed by Network security
on
May 27, 2019
Rating:
No comments: