IMG-LOGO

JQuery handy code snippets

andy - 02 May, 2014 3228 Views 0 Comment

Are you lazy to google the quick jquery code snippet for your website? Look no further, we have included a quick summary list of jquery code to help you faster coding to save your time.

Below codes are the top searchable jquery snippet code that may helpful for your needs.

JQuery code to check if object is visible.

	// using object ID
	var ifVisible = $("#objectID").is(":visible");

	// using className
	var ifVisible = $(".className").is(":visible");

JQuery code to check if checkbox is checked.

	//using object ID
	var ifTicked = $("#objectID").is(":checked");

	//another alternative
	var ifTicked = $("#objectID").prop("checked");

	//another alternative
	var ifTicked = $("#objectID").attr("checked");

	//Use each method to see how many checkboxes are checked
	//let assume the checkboxes located inside a div id #checklist
	$("#checklist input:checkbox").each(function(){
		if($(this).is(":checked")){
			//the item is ticked, do something here...
		}
	});

	//Use each method to set the all checkboxes ticked
	//let assume the checkboxes located inside a div id #checklist
	$("#checklist input:checkbox").each(function(){
		$(this).prop("checked", true);
	});

JQuery code to if object is exist.

We use length property in here to check if an element is exists. Note: if the object is found, it will return a value more than 0.

	// using object ID
	var ifExist = $("#objectID").length;

	// using className
	var ifExist = $(".className").length;

JQuery code to remove all select options.

	//assuming we have a selectbox with ID myselect
	$('#myselect').children().remove().end();

	//if you want to add initial value, you can use this one
	$('#myselect').children().remove().end().append('<option value="0">Select</option>');

JQuery code to remove select option by id value or by text.

	//remove option based on select id value.
	//assuming we have a selectbox with ID myselect and we want to remove value equal to 'apple'
	$("#myselect option[value='apple']").remove();

	//Remove the select option contains word 'bread':
	$("#myselect option:contains('bread')").remove();

JQuery code to add or remove css class.

	//assuming we have a div element with ID myDIV
	//we add class to myDiv object
	$("#myDiv").addClass("myCssClass");

	//Remove css class:
	$("#myDiv").removeClass("myCssClass");

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

Become a jquery expert in just 10 minutes.

Completely new to JQuery script Don t worry you are not alone We have summarize a list of tips for newbie to learn JQuery faster The best way to strength your JQuery skills is by testing our tips directly in ...