WEBSITE

Sharepoint 2013 : Introducing jQuery for SharePoint developers (part 2) - Understanding jQuery methods,Understanding jQuery event handling

7/18/2014 4:41:01 AM

Understanding jQuery methods

Once you have selected DOM elements, you will want to manipulate them. This is where jQuery methods come into play. The jQuery library has a tremendous number of methods that perform all kinds of useful DOM manipulations. These manipulations are always performed on the collection of elements returned from the jQuery global function. Table 2 shows some commonly used jQuery methods.

Table 2. Common jQuery methods

Method

Example

Description

Read the HTML within an element

var x = $("#displayDiv").html();

Returns the inner HTML of the element whose ID is “displayDiv”

Modify the HTML within an element

$("#displayDiv").html("<p>Hello</p>")

Sets the inner HTML of the element whose ID is “displayDiv”

Read the text of an element

$("ul#displayList > li:first").text();

Returns the text of the first list item in the unordered list whose ID is “displayList”

Modify the text of an element

$("ul#displayList > li:first").text("Item 1");

Sets the text of the first list item in the unordered list whose ID is “displayList”

Read the value of a style property

var x = $("#displayDiv").css("marginTop");

Returns the value of the “margin-top” CSS property for the element whose ID is “displayDiv”

Set the value of a style property

$("#displayDiv").css("marginTop","5px");

Sets the value of the “margin-top” CSS property for the element whose ID is “displayDiv” to “5px”

Add a CSS class to an element

$("#displayDiv").addClass("emphasis")

Adds the CSS class named “emphasis” to the element whose ID is “displayDiv”

Remove a CSS class from an element

$("#displayDiv").removeClass("emphasis")

Removes the CSS class named “emphasis” to the element whose ID is “displayDiv”

Hide an element

$("#displayDiv").hide()

Hides the element whose ID is “displayDiv”

Show an element

$("#displayDiv").show()

Shows the element whose ID is “displayDiv”

Toggle the display of an element

$("#displayDiv").toggle()

Hides the element whose ID is “displayDiv” if it is visible; otherwise, shows it

jQuery supports many methods for manipulating DOM elements beyond what is shown in Table 2. The complete reference of supported methods is available at http://api.jquery.com/category/manipulation. Furthermore, jQuery methods can be chained together so that you can perform several operations in a single line of code. The following code changes the inner HTML of an element, adds a class, and then displays the result, all in a single line:

$("displayDiv").html("<p>Hello</p>").addClass("emphasis").show();

Understanding jQuery event handling

Along with selecting and manipulating elements, you can use jQuery to attach event handlers to DOM elements. By handling events in jQuery, you can keep your JavaScript code out of the webpage and contained within custom libraries. This approach makes your code much more maintainable and isolated than using a more traditional approach to bind events to DOM elements.

The basic approach for binding events is to select the target DOM element by using the global function and then bind the event. The code to run in response to the event can be defined directly in the binding as an anonymous function. The following code shows a simple example of binding the click event of all paragraph elements:

$("p").click( function (e) {
alert($(e.target).text());
});

Notice in the preceding code that the function handling the click event is defined in line with the binding. Additionally, notice how the element that caused the event can be determined by selecting e.target within the function.

Of all the events available in jQuery, the most important is the ready event of the document object. This event fires when the DOM is ready for selection and manipulation. The SharePoint-hosted app project template in Visual Studio 2012 automatically adds this event handler into the Apps.js library of the app to act as the starting point. This is a pattern that you should follow in your apps, as well.
Other  
  •  Sharepoint 2013 : Introducing JavaScript for SharePoint developers (part 3) - Creating custom libraries
  •  Sharepoint 2013 : Introducing JavaScript for SharePoint developers (part 2) - Understanding JavaScript functions, Understanding JavaScript closures, Understanding JavaScript prototypes
  •  Sharepoint 2013 : Introducing JavaScript for SharePoint developers (part 1) - Understanding JavaScript namespaces, Understanding JavaScript variables
  •  Sharepoint 2013 : List and library essentials - Creating and deleting lists
  •  Sharepoint 2013 : List and library essentials - Using your apps
  •  Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Access Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - The New Machine Translation Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - Word Automation Services and the New PowerPoint Automation Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - What’s New in Excel Services
  •  Sharepoint 2013 : Developing Applications Using Office Services - WOPI and the New Office Web Apps Server
  •  
    Video
    Top 10
    Michael Kors Designs Stylish Tech Products for Women
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone