jQuery Form Events

When a form control receives or loses focus or when form control value is modified by the user such as by typing text in a text input, select any option in a select box etc. then the form event fires. To handle the form events here are some methods.

1) The change() Method

When the value of an element is changed then the jQuery change event occurs. When the change event occurs then the change () method attaches a function with it to run. This event is only used with <textarea> boxes, <input> elements and <select> elements. Syntax:
$(selector).change()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("input").change(function(){  
        alert("The text has been changed.");  
    });  
});  
</script>  
</head>  
<body>  
<h1>this is the Heading </h1>  
Please enter something.<input type="text">  
<p>Write anything in the input field and then click outside the field or press enter.</p>   
</body>  
</html>
Try Now

2) The focus() Method

The focus() method of jQuery attaches an event handler function to an HTML form field. When the form field gets focus then the function executes. Syntax:
$(selector).focus()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("input").focus(function(){  
        $(this).css("background-color", "#ffffff");  
    });  
    $("input").blur(function(){  
        $(this).css("background-color",  "#cccccc?);  
    });  
});  
</script>  
</head>  
<body>   
Enter your First Name: <input type="text" name="fullname">  
Enter your Password:  <input type="text" name="password">    
</body>  
</html>
Try Now

3) The blur() Method

jQuery blur() method is used to remove focus from the element. An event handler function is attached to an html element by the blur() method and that function is executed whenever the form field loses focus. Syntax:
$(selector).blur()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("input").focus(function(){  
        $(this).css("background-color", "#cccccc");  
    });  
    $("input").blur(function(){  
        $(this).css("background-color", "#ffffff");  
    });  
});  
</script>  
</head>  
<body>   
Enter your First Name: <input type="text" name="firstname">  
Enter your Password: <input type="text" name="email">    
</body>  
</html>
Try Now

4) The submit() Method

When a form is submitted then the submit event occurs and after occurring the submit event, submit() method triggers the submit event or attaches a function to run. Syntax:
$(selector).submit()

Example:

<!DOCTYPE html>  
<html>  
<head>  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
<script>  
$(document).ready(function(){  
    $("form").submit(function(){  
        alert("Submitted");  
    });  
});  
</script>  
</head>  
<body>   
<form action="">  
  Enter your Name: <input type="text" name="name">  
  Enter your Email: <input type="text" name="email">    
<input type="submit" value="Submit">  
</form>    
</body>  
</html>
Try Now 

Related Topics

jQuery Introduction

jQuery means "write less do more". It is a lightweight, "write less, do more" and JavaScript library created by John Resig in 2006. jQurey is used to simplify the client-side...

1 minute read.

Getting and setting content

Retrieving and setting text, values and HTML is the simplest aspect of manipulation of DOM. It seems same but they’re not. There are text(), html() and val() methods. jQuery html() method Using the...

4 minutes read.

jQuery Tutorial

jQuery tutorial provides deep knowledge of jQuery technology for both beginners and professionals. In this jQuery tutorial you will learn about example, selectors, traversing, events, effects, jQuery fundamentals, CSS and attributes....

3 minutes read.

jQuery Fading Effect

jQuery Fade Using jQuery, you can fade elemnts in and out of visibility. You can change the opacity level of elements from invisible state to visible state or visible state to...

6 minutes read.

jQuery Form Events

When a form control receives or loses focus or when form control value is modified by the user such as by typing text in a text input, select any option...

3 minutes read.

jQuery Get()/Post() Methods

The jQuery get() and post() methods is used to send a HTTP request to a page and get the result back from the web server. GET and POST are the two...

3 minutes read.

Introduction to AJAX

AJAX stands for Asynchronous JavaScript and XML. In AJAX, the data is loaded in the background and displayed on the webpage. In this process, you don't need to reload the...

1 minute read.

jQuery Document/Window Events

When the user resizes or scrolls the browser window then the events triggers. Here are some jQuery methods to handle the document/ window events: 1) The ready() Method JQuery ready() method is...

2 minutes read.

Introduction to DOM manipulation

Manipulation of DOM is one of the most important aspects of jQuery and thereby JavaScript. DOM stands for Document Object Model. It is a manipulation of interacting and representing with...

1 minute read.

jQuery Keyboard Events

When the user press or release a key on the keyboard then the keyboard event fires. Here are some jQuery methods to handle the keyboard events. 1) The keypress() Method: When a keyboard...

2 minutes read.

jQuery Stop Effect

The jQuery stop() method is used to stop the jQuery animation or effect before it is finished. The jQuery stop() method works for all jQuery effects like: custom animations, fading and...

1 minute read.

jQuery Add

Using the jQuery, it is easy to add new content. Here are four jQuery methods to add new content: append() prepend() after() before() jQuery append() Method The jQuery append() method is used to insert the content...

4 minutes read.

jQuery Example

Hello, world! Example Here is the jQuery first example: <div id="divTest1"></div>   <script type="type/javascript">   $("#divTest1").text("Hello, world!");   </script> The above code have a div tag with the id of "divTest1" and $ shortcut to access jQuery. In this code select all...

2 minutes read.

jQuery hide/show Effect

jQuery provides a interface for doing various kind of amazing effects like Slide, Fade, Hide, Show, Toggle Effects and Animate. jQuery hide() and show() Using the jQuery show() and hide() methods, you...

2 minutes read.

jQuery CSS() Method

Using the jQuery css() method, you can set or return the one or more style properties for the selected elements. Here are the two ways to provide jQuery css() method: Set a...

2 minutes read.

jQuery Remove

Using the jQuery, it is easy to remove the existing HTML content. There are two jQuery methods to remove the content: remove() empty() jQuery remove() Method The jQuery remove() method is used to remove the...

2 minutes read.

jQuery load() Method

jQuery load() method is a powerful AJAX method used for loading data asynchronously. The load() method loads HTML or text content from a server and added into a DOM element. Syntax: $(selector).load(URL,data,callback); Here is...

2 minutes read.

How to use jQuery

If you want to use jQuery then firstly include it on the pages where you wish to take advantage of it. To do this you have to download the jQuery....

2 minutes read.

jQuery Selectors

When you use the JavaScript then read and modify the content of the page is the very common task. While doing this you want to find the elements that you...

3 minutes read.

jQuery Events

jQuery event is used to show a moment when something happens. jQuery is used to set up event-driven responses on the HTML page elements. These events have often triggered by the...

1 minute read.