var fieldValue = $(‘#user’).val(); // Get the value of the form field with id ‘user’
var fieldValue = $(‘#user’).val(‘Value to set’); // Set the value of the form field with id ‘user’
var fields = document.getElementsByTagName(‘input’); // Select all form fields
$(‘:input’).attr(‘disabled’,true); // Disable input fields
To select all form fields of a given type in JQuery:
$(‘:input’) Selects all input, textarea, select, and button elements. In other words, it selects all form elements.
$(‘:text’) Selects all text fields.
$(‘:password’) Selects all password fields.
$(‘:radio’) Selects all radio buttons.
$(‘:checkbox’) Selects all checkboxes.
$(‘:submit’) Selects all submit buttons.
$(‘:image’) Selects all image buttons.
$(‘:reset’) Selects all reset buttons.
$(‘:button’) Selects all fields with type button.
$(‘:file’) Selects all file fields (used for uploading a file).
$(‘:hidden’) Selects all hidden fields.
You can combine form selectors as follows: $(‘#signup :text’) // Select ‘signup’ element but only text input fields from those selected
$(‘:checked’) Select all checkboxes that have been checked
if ($('#news').attr('checked')) {
// the box is checked
} else {
// the box is not checked
}
var selectedState=$('#state :selected').val(); // Gets the value of the selected fields of 'states'
Example of submit event:
$(document).ready(function() {
$('#signup').submit(function() {
if ($('#username').val() == '') {
alert('Please supply a name in the Name field.');
return false;
}
}); // end submit()
}); // end ready()
Example of focus event:
$('#username').focus(function() {
var field = $(this);
if (field.val()==field.attr('defaultValue')) {
field.val('');
}});
Example of change event:
$('#country').change(function() {
if ($(this).val()=='Please choose a country') {
alert('Please select a country from this menu.');
}
}