JQuery Textbox

Textbox are used to collect user entered data. Mainly it is used along with other componenets to collect data.
  • Video Tutorial on JQuery TextBox

Reading the data entered in textbox

Our text box id=t1, here is the code to read the value entered inside a text box.
var str=$('#t1').val();
Here we stored the text box data in a variable str.
We can display the data entered by connecting it to a button click function.
HTML part
<input type=text name=my_text id=t1><button name=b1 id=b1>Copy</button>
<div id=d1></div>
$(document).ready(function() {
var str=$('#t1').val();

Show or Hide the Text Box

$('#t1').show(); // Display the hidden text box with id = t1
$('#t1').hide(); // Hide the text box
Read more on Show & Hide elements

Disable the text box

We can disable a text box or enable it by managing prop
$("#t2").prop("disabled", false);
Here the button with id b2 on Click will disable the text box , same time button with id b3 will enable it by using click function.
HTML part
<input type=text name=my_text id=t2>
<button name=b2 id=b2>Disable</button></div>  
<button name=b3 id=b3>Enable</button> 
$(document).ready(function() {
$("#t2").prop("disabled", true);
$("#t2").prop("disabled", false);


Disable textbox based on selection of checkbox

We can connect the textbox to a checkbox where on selection of checkbox the textbox can be enabled. By default we will keep the textbox disabled.

<input type=checkbox id='ckb'> <input type=text name=my_text id=t3> 
<script>$(document).ready(function() {
$("#t3").prop("disabled", true);

  var ckb_status = $("#ckb").prop('checked');
$("#t3").prop("disabled", false);
$("#t3").prop("disabled", true);

Setting the value of textbox

$("#t4").attr('value','Default Data');

Making the value empty


Set Focus


Selecting value inside textbox

$("#text_id").select(); // Select all data inside textbox with id =text_id

Getting id and value of group of text box with common class

We have several textboxes with different id and different value but with common class. Here is how we will collect the id and value of the textbox which the user put the crusher or the textbox in focus.
We will use blue event of the group to hide the display of value.
We have used one div element ( id = display ) to show the collected id and value of the textbox.
Here is the code.
var id=this.id; // Collect id 
var id=$(this).attr('id') // Collect id
var val=$(this).val(); // Collect value

$("#display").html('id:'+id+', value:'+ val);

Adding Style to Textbox

$('#t1').css('border-color', 'red'); // Border color red 
$('#t1').css('border-color', ''); // Remove border color 
// Background colour ///
$('#t1').css('background-color', 'green'); // Add background color
$('#t1').css('background-color', ''); // Remove background color

Video Tutorial code

<title>(Type a title for your page here)</title>
<input type=text id=t1> <button id=b1> Display </button> 
<input type=text id=t2><br>
<span id=display></display>

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
$(document).ready(function() {
// Jquery code here ///
var data=$('#t1').val();
if(data.length <=3){
var msg="Minimum length is 3 ";
$('#t1').css('border-color','red'); // border color
$('#t1').css('background-color','#f1f1f1'); // background color
$('#t1').css('border-color','green'); // border color
$('#t1').css('background-color','#ffffff'); // background color
var msg="OK.";
$('#display').html('Enter your name');
Textbox Code Generator using Bootstrap style DEMO : Group of Textbox Managing Button Radio button


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here .

    Most Popular JQuery Scripts


    Two dependant list boxes


    Calendar with Date Selection


    Data change by Slider


    Show & Hide element

    JQuery Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer