jQuery prop() method

The jQuery prop() method get the value of a property for the first element in the set of matched elements or set one or more properties for every matched element.

.prop( propertyName )

Description : Get the value of a property for 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="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>

<button id="button1">Get Property Value</button>
  
<script>
jQuery("#button1").click(function () {
	if(jQuery("#check1").prop('checked')) {
		jQuery("#check1").wrap('<span></span>').parent().css({border:"3px #1E8CBE solid"});
	}
});
</script>
</body>
</html>

Demo

Following example checks “checked” property of the checkbox (with id=”check1″).


.prop( propertyName, 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="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>

<input id="check2" type="checkbox" checked="checked">
<label for="check2">Check me</label>

<button id="button2">Set Property Value</button>

  
<script>
jQuery("#button2").click(function () {
	jQuery("#check2").prop("checked", false);
});
</script>
</body>
</html>

Demo

Following example sets “checked” property of the checkbox (with id=”check2″) to false.



,

No comments yet.

Leave a Reply