Bookmark and Share

Monday, November 30, 2009

JQuery assorted tips

Here are some useful JQuery 1.2.7 code snippets:

//Select the cheched checbox:
//Get an array of text of the selected SELECT
var elems = $.map($('#myId).find('select :selected'),function(a){return $(a).text();})
//Get an array of the values of the selected SELECT
var txtelems = $.map($('#myId).find('[@id=innerElementId]').find('input'),function(a){return $(a).val();});
//Set the title on a element
$(idTxtSearch).attr('title','my title');
//Focus a textbox (INPUT field)
//reset the value of a textbox (INPUT field)
//not JQuery, add a trim function to the JavaScript String object
String.prototype.trim = function ()
return this.replace(/^\s*/, "").replace(/\s*$/, "");
//ASP.NET + JQuery + blockUI
//Call the postback on a button after having blocked the page
//Note that the __doPostBack requires the name of the element, not the Id, rules.
__doPostBack($('#<%= myAspNetBtn.ClientID %>').attr('name'), '');
//Attach the keyup event of a textvox
//function for the blur and focus events
//reset all the checkboxes inside an element
$('#myid > input[@type=radio]').attr('checked','')
//find the first DIV inside a table row that contains a value
if ($('#myid').find('tr:contains(thestring)').find('div:eq(1)').length>0)
//reset an element and add some HTML
//find all visible elements inside an element with class myClass
//change the source image on some images 
//add the nowrap attribute to all TDs
//hide an element 
//disable a button
//find the first element with the same attribute and get the display attribute
//Toggle a class on a element
//Execute script after the page load
//some js here;
//find the first image inside an element and chenge the image source
//duplicate an HTML element
//get the url of an anchor with id=myId
var sUrl = $("a[@id='myId']").attr('href');
//hide all anchors inside an element
//get the text of an element
var text = $('#myId').text();
//disable a button
$('#myId').attr('disabled', true);
//click a button after the load of the page
//check all visible checkboxes inside a table
function selectAllVisibleCheckBox(idTable)
$("#" + idTable).find("input[@type$='checkbox']").each(function()
if ($(this).parents("TR").css("display")!='none')
return false;
//call the classical HelloWorld asmx web service using JQuery
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebServices.asmx/HelloWorld",
data: "{'msg':'hello!'}",
dataType: "json"