By: aathishankaran in JavaScript Tutorials on 2007年03月27日 [フレーム]
In JavaScript, the following are the common event handlers for handling text input:
onblur: The onblur event occurs when an element loses focus, and is often used to validate form inputs.Example:
<input type="text" id="myInput" onblur="validateInput()">
<script>
function validateInput() {
var input = document.getElementById("myInput");
if (input.value === "") {
alert("Input cannot be empty");
}
}
</script>
onchange: The onchange event occurs when the value of an element changes, and is often used to perform an action when a user selects an option from a dropdown list.Example:
<select id="mySelect" onchange="changeColor()">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
<script>
function changeColor() {
var select = document.getElementById("mySelect");
var color = select.value;
document.body.style.backgroundColor = color;
}
</script>
onselect: The onselect event occurs when a user selects text in an input field or textarea, and is often used to perform an action on the selected text.Example:
<textarea id="myTextarea" onselect="highlightText()"></textarea>
<script>
function highlightText() {
var textarea = document.getElementById("myTextarea");
var selectedText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
alert("Selected text: " + selectedText);
}
</script>
onfocus: The onfocus event occurs when an element gains focus, and is often used to perform an action when a user clicks on an input field or textarea.Example:
<input type="text" id="myInput" onfocus="showHint()" onblur="hideHint()">
<div id="hint" style="display: none;">Enter your name</div>
<script>
function showHint() {
var hint = document.getElementById("hint");
hint.style.display = "block";
}
function hideHint() {
var hint = document.getElementById("hint");
hint.style.display = "none";
}
</script>
These are just a few examples of how to handle text input events in JavaScript. There are many more events and event handlers that can be used to create dynamic and interactive web applications.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in JavaScript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in JavaScript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate