val()
MethodYou can use the val()
method to test or check if inputs are empty in jQuery.
The following example will add a red outline around the inputs if it is focused but not filled out.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Check If Inputs are Empty using jQuery</title>
<style>
.error{
outline: 1px solid red;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$('#myForm input[type="text"]').blur(function(){
if(!$(this).val()){
$(this).addClass("error");
} else{
$(this).removeClass("error");
}
});
});
</script>
</head>
<body>
<form id="myForm">
<p><label>First Name: <input type="text"></label></p>
<p><label>Last Name: <input type="text"></label></p>
<p><label>Email Address: <input type="text"></label></p>
</form>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Node.js MySQL Create table into Database Server
This article will guide you to how to cr...Laravel 8 Authentication using Livewire Jetstream
Today, let's optically discern the p...Google reCAPTCHA V3 Tutorial with Example in PHP
Google reCAPTCHA is tool by Google which...How to get and set Value in Form Fields in Javascript
In this article, we will discuss on sett...How to populate state dropdown based on option selected in country dropdown using jQuery
Use the jQuery ajax() method...