Jquery Post

Contains common Jquery code

* Display jQuery version

alert(jQuery.fn.jquery);

* Find how many check boxes have been checked

var count = $(‘input[type=checkbox]:checked’).length;

* Get ID, class of an element

var element_id = $(this).attr(‘id’); or element_id = this.id;
var class = $(this).attr(‘class’); or class = this.class;

* Set the attribute of an element

$(this).attr(‘class’, ‘new-class’);
$(this).attr(‘title’, ‘new-title’);

* Set multiple attributes for an element

$(‘this’).attr({
class: ‘new-class’,
title: ‘new-title’
});

* Checking and Un-checking a check-box

this.checked = false; //un-check
this.checked = true; //check

* Loop through all check-boxes and find if they are checked

$(‘input[type=checkbox]’).each(function () {
var is_checked = (this.checked ? “1” : “0”);
});

* Loop through all radio buttons in a form

$(“#form-id”).find(‘:radio’).each(function() {
radio_id = $(this).attr(‘id’);
radio_name = $(this).attr(‘name’);
});

* PHP like explode and implode functions in Javascript

//explode
var my_string = “abc-def-ghi-jkl”;
var my_array = [];
my_array = my_string.split(“-“);

//implode
var my_array = new Array(‘abc’, ‘def’, ‘ghi’, ‘jkl’);
var my_string = “”;
my_string = my_array.join(“-“);

# Disable all radios of a group

$(‘input:radio[name=radio-group-name]’).attr(‘disabled’, true);

# Get the value of the selected radio button in a group

$(‘input:radio[name=radio-group-name]’).val();

# Disable all input elements in a form (input, textarea, select and button)

$(“#form-id :input”).attr(“disabled”, true);

# Disable only input elements of a form

$(“#form-id input”).attr(“disabled”, true);

# Get the current page url

var current_page_url = window.location.pathname;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s