You are on page 1of 3

bind( event , {key : value, key : value}, function); $( a ).bind( click , function() { alert($(this).

text()); }); var msg = "msgvalue1"; $("a").bind("click", {message : msg}, function(event) { msg = "msgvalue2"; alert(event.data.message); }); Binds function to event for the selected element. Example 1 equivalent to: <a onclick="AlertLinkText()">Link Text</a>. Example 2 binds the click event to all a tags on the page and adds data to the event using bind()'s optional second parameter. This is a map of keys and values which can then be accessed as sub-properties of the event's data propert y. This allows you to pass outside complementary information into the event. Not e that even though msg is changed inside the event handler function, the alert d isplayed will still be msgvalue1 as the message is evaluated as soon as the even t handler is bound, which is as soon as the page is loaded. -------------------------------------------------------------------------------------------------------unbind("event", function); "$(function() { var handler1 = function() { alert(""First event handler!""); } var handler2 = function() { alert(""Second event handler!""); $(""a"").unbind(""click"", handler2); } $(""a"").bind(""click"", handler1); $(""a"").bind(""click"", handler2); });" Unbinds function from event. function is optional - providing only the event(s) to be unbound will unbind all functions associated to those event types in the s elected elements. In the example, you will only see the handler2 alert the first time you click a link on that page as handler2 specifically unbinds itself from the click event of all a tags on the page whereas the handler1 alert will conti nue to appear each time a link is clicked as it is never unbound. ------------------------------------------------------------------------------------------------------------live( event , {ke : value, key : value}, function);

Same effect as bind, but will attach event handlers to all new elements of the t ype selected, even after the page has loaded. ------------------------------------------------------------------------------------------------------------die("event", function); Should be used when live() has been used as opposed to bind() ------------------------------------------------------------------------------------------------------------load("contentURL optionalSelector", { key : value }, callbackFunction(responseTe xt, statusText, returnedXhrObject){}); contentURL: URL of page that you wish to insert into the selected el ement. optionalSelector: optional jQuery selector - only the selected elements wi ll be returned from the content file. responseText: Contains the resulting content if the load call succeeds . statusText: Contains a string of value "success" or "error" dependin g on whether the connection was successful. Can be used to test whether a connection was successful or not. returnedXHRObject: The XmlHTTPRequest object that was used to perform the c all - contains properties useful for debugging. ------------------------------------------------------------------------------------------------------------$.get("contentURL.php[opt]?callback=?", callbackFuntion(data, textStatus){}, "re turnFormat"); contentURL: URL of page that you wish to insert into the selected el ement. The callback variable allows you to call a method on response. Use ? for an anonymous function included in the get() function call. data: Data returned by the page addressed in the URL. textStatus: Contains a string of value "success" or "error" depending on whether the connection was successful. Can be used to test whether a connection was successful or not. returnFormat: String specifying the returned format desired. Use of "j son" will allow requests to other domains, which get blocked by the Same Origin Policy of each browser. <ul id="ulUsers"></ul> <script type="text/javascript"> $(function() { $.get ( "http://tests.jquery-tutorial.net/json.php?callback=?", function(data, textStatus) { $.each(data, function(index, user) {

$("#ulUsers").append($("<li></li>").text(user.na me + " is " + user.age + " years old")); }); }, "json" ); }); </script> PHP file this request was sent to: <?php $users = array ( array("name" => "John Doe", "age" => 42), array("name" => "Jane Doe", "age" => 39) ); echo $_REQUEST['callback'] . "(" . json_encode($users) . ")"; ?> Example requests the result of json.php then loops through the returned array us ing the each() function, appending each line of data to ulUsers as a list item.

You might also like