Thursday, March 1, 2012

Useful jQuery Tips

Check if a checkbox is checked:
if($('#checkbox').is(':checked')) ...

Check if a select option is selected:
if($('#select option:selected').length()) ...

Get the selected radio value:
$('.radio:checked').val()

Get all inputs with a classname begins with rescheck:
  $("input[class^=rescheck-]").each(
    function() {
      alert($(this).attr('class'));
    }
  );

Jump to ( Scroll To ) an element:
$('html, body').animate({ scrollTop: $("#some_element").offset().top }, 500);

Checking to see if there is an attribute on an element:
var attr = $(this).attr('name');
if (typeof attr !== 'undefined' && attr !== false) {}

Clear selection on select:
$("#my_select option:selected").removeAttr("selected");

Ajax request and putting the result array into a select:

  $.post("/url", function(data) {
    var selected = '';
    $.each(data, function(key, data) {
      $('#foobar').append("");
    });
  });

No comments:

Post a Comment