jQuery val() method

The jQuery val() method get the current value of the first element in the set of matched elements or set the value of every matched element.

.val()

Description : Get the current value of the first element in the set of matched elements.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
   <style>
 	 .highlight { border : 2px solid #1E8CBE; }
  </style>
</head>
<body>
  
<input id="text1" type="text" value="Hello World!!!"></input>

<button id="button1">Get Value</button>
  
<script>
jQuery("#button1").click(function () {
	alert(jQuery("#text1").val());
});

</script>
</body>
</html>

Demo

Following example find the value of an input box.

.val( value )

Description : Set one or more properties for the set of matched elements.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
   <style>
 	 .highlight { border : 2px solid #1E8CBE; }
  </style>
</head>
<body>
  
<input id="text2" type="text" value="Hello World!!!"></input>

<button id="button2">Set Value</button>
  
<script>
jQuery("#button2").click(function () {
	jQuery("#text2").val("Hello Puneet!!!");
});
</script>
</body>
</html>

Demo

Following example set the value of an input box.

,

No comments yet.

Leave a Reply