Skip to main content

Event in JavaScript


It is used to perform an action in HTML elements, event will be defined as a method and call under HTML elements using event action.
for example, onclick is the event that will be called when we click on the button.
Javascript is called an event-driven based language.

Javascript has multiple event methods

1) onkeypress  --->  when we press keyboard any button then it will be called
2) onkeyup  ------> when we press any key then the first onkeyup event will be called
3) onmouseover --->  when we over the mouse under the HTML element then this event will be called
4) onmouseout --->  when we remove mouse control from the HTML element then this event will be
5) onfocus ------>    When cursor will be assigned into a particular HTML element then this event will be called
6) onblur  ---->      When we remove the cursor from the HTML element then this event will be raised
7) onclick  ----->     When we click on Button then onclick event method will be called
8) onchange  ---->  when we select dropdown list or Listbox item then on-change will be called.
Example of onmouseover and onmouseout:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function fun1()
{
            document.getElementById("btn1").style.backgroundColor='red';
            document.getElementById("btn1").style.width='200px';
            document.getElementById("btn1").style.height='30px';
}
function fun2()
{
          document.getElementById("btn1").style.backgroundColor='';
          document.getElementById("btn1").style.width='';
            document.getElementById("btn1").style.height='';
}
</script>
</head>
<body>
  <input type="button" id="btn1" value="Click" onmouseover="fun1()" onmouseout="fun2()" />
</body>
</html>
Example of OnFocus and Onblur:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function fun1()
{
            document.getElementById("txt1").style.backgroundColor='green';
            document.getElementById("txt2").style.backgroundColor='red';
        }
function fun2()
{
          document.getElementById("txt1").style.backgroundColor='red';
         document.getElementById("txt2").style.backgroundColor='green';
}
</script>
</head>
<body>
  <input type="text" id="txt1" onfocus="fun1()" onblur="fun2()"     />
   <input type="text"  id="txt2"   />
</body>
</html>

........................................................................................................................................................

Example of Keyboard based event:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function fun(data)
{
//alert(a);
document.getElementById("res").innerHTML=data;
}
</script>
</head>
<body>
<input type="text" id="txt1" onkeyup="fun(this.value)" />
<div id="res"></div>
</body>
</html>

............................................................................................................................................

Example of onchange using dropdownlist:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
r1=" ";
r2=" ";
function fun(data)
{
         r1=data;
         document.getElementById("res").innerHTML=r1+r2;
}
        function fun1(data)
{
         r2=data;
         document.getElementById("res").innerHTML=r1+r2;
}
</script>
</head>
<body>
<select id="country" onchange="fun(this.value)">
<option value="">Select Country</option>
<option value="INDIA">INDIA</option>
<option value="USA">USA</option>
<option value="CHINA">CHINA</option>
<option value="JAPAN">JAPAN</option>
</select>
<br><br>
<select id="state" onchange="fun1(this.value)">
<option value="">Select State</option>
<option value="MP">MP</option>
<option value="UP">UP</option>
<option value="AP">AP</option>
<option value="HP">HP</option>
</select>
<div id="res"></div>
</body>
</html>

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

onchange example using RadioButton:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function fun(data)
{
         document.getElementById("res").innerHTML=data;
}
</script>
</head>
<body>
<input type="radio" name="r1" id="r1" value="Male" onchange="fun(this.value)">Male
<br><br>
<input type="radio" name="r1" id="r1" value="Female" onchange="fun(this.value)">Female
<br><br>
<div id="res"></div>
</body>
</html>
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Example of  CheckBox:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
c1="";
c2="";
function fun1()
{
if(document.getElementById("c1").checked)
{
          c1 =document.getElementById("c1").value;
}
else
{
           c1="";
}
         document.getElementById("res").innerHTML=c1+c2;

}
function fun2()
{
if(document.getElementById("c2").checked)
{
          c2 =document.getElementById("c2").value;
}
else
{
           c2="";
}
         document.getElementById("res").innerHTML=c1+c2;

}
</script>
</head>
<body>
<input type="checkbox" name="c1" id="c1" value="C" onchange="fun1()">C
<br><br>
<input type="checkbox" name="c2" id="c2" value="CPP" onchange="fun2()">CPP
<br><br>
<div id="res"></div>
</body>
</html>
Example of Checkbox and Listbox:-
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function fun()
{
            s = "";
            console.log("click"+document.frm.chk.length);
            for(i=0;i<document.frm.chk.length;i++)
            {
           
                if(document.frm.chk[i].checked)
                  {
                  s = s+ document.frm.chk[i].value;
                  }
            }

           s1="";
           for(i=0;i<document.getElementById("lst[]").length;i++)
            {
           
                if(document.getElementById("lst[]")[i].selected)
                  {
                  s1 = s1+ document.getElementById("lst[]")[i].value;
                  }
            }
            document.getElementById("res").innerHTML = "name is " + document.frm.txtname.value+s + "Course is "+s1;
}
</script>
</head>
<body>
<form name="frm" action="">
<input type="text" name="txtname" /> <br>
<input type="checkbox" name="chk" value="C" />C <br>
<input type="checkbox" name="chk" value="C++" />C++ <br>
<input type="checkbox" name="chk" value="DS" />DS <br>
<br><br>
<select id="lst[]" multiple="true" style="width:300px;">
<option value="C">C</option>
    <option value="C++">C++</option>
    <option value="DS">DS</option>
    <option value="JAVA">Java</option>
    <option value=".NET">.NET</option>
</select>
<br><br>
<input type="button" id="btnclick" onclick="fun()" value="Click" />
<br>
<div id="res"></div>
</form>
</body>
</html>

Example of Javascript to Change Background Image:-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
       function fun(a)
       {

         //  console.log(a);

           document.getElementById("body").style.backgroundImage="url('"+a+"')";

       }


    </script>
</head>
<body id="body">
    <select onchange="fun(this.value)">
      <option value="">Select Theme</option>
      <option value="bg1.jpg">Theme1</option>
      <option value="bg2.jpg">Theme2</option>
      <option value="bg3.jpg">Theme3</option>
    </select>
</body>
</html>
                                                    

Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...