jQuery enable-disable elements

Let’s summarise here how to disable, enable, check, assign values and get selected values from your html document.

1
2
3
4
5
<select name="client.idClient" id="idClient">
	<option value="1">CLIENT 1</option>
	<option value="2">CLIENT 2</option>
	<option value="3">CLIENT 3</option>
</select>

You can disable an element by setting the disabled attribute to disabled:

1
jQuery("#idClient").attr("disabled","disabled");

And enable the element by removing the disabled attribute:

1
jQuery("#idClient").removeAttr("disabled");

If the element is an input:

1
<input type="checkbox" id="yes"/>

then you have to set the checked attribute to checked:

1
2
jQuery("#yes").attr("checked","checked");
jQuery("#yes").removeAttr("checked");

If you want to set a value for an element in your document:

1
jQuery("#idClient").val('1');

will set the selected option in the dropdown to the value 1.

To get the selected value:

1
jQuery("#idClient").val();

and the selected text:

1
jQuery("#idClient option:selected").text();

Very useful link: jQuery – FAQ

Popularity: 32% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
You can leave a response, or trackback from your own site.

Leave a Reply