6/11/2016

jQuery frequently used syntax

// Gets all tags in a div
$('#divId img')

Or if the div jQuery object is available
$div.find('img')

// Ajax calls. Don't use $.get or $.post because they don't have error handling
$.ajax();

// Gets attribute
$('#divid').attr('src')
$('#divid').attr('src', 'img.jpg')
$('#divid').attr('width')

// CSS
$('#divid').css('visibility', 'visible');
$('#divid').css('visibility', 'hidden');
$('#divid').css('left', '100px');
$('#divid').css('width', '100px');

// Gets/Sets the html
$('$divid').html()
$('$divid').html('')

// Gets the DOM element
$('#divid').get()  // an array of elements
$('#divid img').get(0)

// Change element class. NOT .attr('className', 'selected')
$('#divid').addClass('selected');
$('#divid').removeClass('selected');

// Add listener
$('#divid').on('click', function() {});
$('#divid').off('lick', function() {});
#(document).on('click', function() {});

// Creates a div
$('<div></div>').addClass('line').appendTo('body');

// Remove a div
$('#divid').remove();

// Select option
$('#selectid').append(new Option(text, value));

$('#selectid').val() to get the selected value.

No comments: