Monday 3 March 2014

JQuery selectors and how to use them

Get selected text from dropdown

$("#myid option:selected").text();

Get selected value from dropdown

$("#myid option:selected").value();

Get input type text value

$("#myid input[type=text]").val();

Get checked radiobutton value by class

$(".myclass").click(function() {
  var input = $('.myclass:checked').val();
  alert(input);
});

Check whether radiobutton or checkbox checked or not

$("#test").click(function() {
   var check = $('#test').attr('checked');
   if(check === "checked")
        //condition
   else
        //condition
});

Clear all form fields (i.e. text-field, file-field, drop-down, check-box and radio-buttons)

$('input[type="text"]').val('');
$('input[type="file"]').val('');
$('input[type="radio"]').prop('checked', false);
$('input[type="checkbox"]').prop('checked', false);
$("#mydropdown option:selected").prop("selected", false);

If you want to exclude any one field then you can do like this. Ex. you want to exclude one drop-down field

Option1:

$('select[name != "test"]').val('');

Option2:

$("select:not([name='User[name]'], [name='User[email]'])").val("");

Event based on the value of data attribute

$( "input[data-role-type='privilege_role']" )

No comments:

Post a Comment