WEBSITE

Sharepoint 2013 : Introducing jQuery for SharePoint developers (part 1) - Referencing jQuery, Understanding the global function, Understanding selector syntax

7/18/2014 4:40:33 AM

In the same way that developers can build and reuse their own JavaScript libraries, third parties have created JavaScript libraries that can simply be referenced and used in app development. Although there are many third-party libraries available on the Internet, one library, jQuery, is so popular that it has almost become synonymous with JavaScript itself.

The reason for the popularity of jQuery is that it does two very important things extremely well: it makes it easy to select elements from the document object model (DOM) and then perform operations on the selected elements. jQuery is so important that Microsoft has baked it into the app project template in Microsoft Visual Studio 2012. Therefore, SharePoint app developers must understand how to use the jQuery library. The following sections present a brief introduction to jQuery from the perspective of a SharePoint app developer. Readers who want complete coverage of the library should visit the jQuery website at www.jquery.com.

Referencing jQuery

To include any JavaScript library in an app, it must be referenced by using a script tag. The script tag refers to the location of the library so that it can be downloaded. In the Visual Studio app project template, the jQuery library is included as a file and referenced in the Default.aspx page, as shown in the following code:

<script type="text/javascript" src="../Scripts/jquery-1.6.2.min.js"></script>

Along with directly hosting the jQuery library in your app, you can also choose to use a content delivery network (CDN). A CDN hosts the jQuery library in the cloud so that it is always available. Referencing a CDN can improve performance of public-facing apps because the library is downloaded in parallel and cached. The same version of the library can then be used across several different apps. The following code shows how to reference the Microsoft CDN for jQuery:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/java
script">
</script>

Understanding the global function

The jQuery library is encapsulated in a single function named jQuery, which is known as the global function. Using the global function, you can easily select elements from the DOM, which is fundamental to any JavaScript solution. To select DOM elements, the global function is invoked and selector syntax is passed. The following code shows the traditional method of selecting elements in JavaScript by using the getElementById method, contrasted with the jQuery approach:

var elem1 = document.getElementById("displayDiv");
var elem2 = jQuery("#displayDiv");

In the preceding code, the jQuery selector syntax uses the hash sign to indicate that the selector corresponds to the ID of the desired element. You can simplify this code even further because the jQuery library uses the $ symbol as an alias for the global function. Therefore, the following code is equivalent:

var elem1 = document.getElementById("displayDiv");
var elem2 = $("#displayDiv");

Understanding selector syntax

At first, it might seem that selecting DOM elements by using jQuery is not that exciting. The power of jQuery, however, lies in the fact that the selector syntax is identical to that used in cascading style sheets (CSS). This means that you can use a rich, familiar selector syntax to reference any part of the DOM, which becomes a powerful and efficient way to manipulate the DOM elements. Table 1 shows common selection operations and how to accomplish them in jQuery.

Table 1. jQuery selector syntax

Operation

Example

Description

Select elements by type

$("p")

Selects all paragraph elements in the page

Select elements by ID

$("#container")

Selects the element whose ID is “container”

Select elements by class

$(".emphasis")

Selects all elements with a class attribute of “emphasis”

Select elements by type and ID

$("div#displayDiv")

Selects the <div> element whose ID is “displayDiv”

Select elements by ancestor and descendant

$("div#displayDiv p")

Select all paragraph elements within the <div> whose ID is “displayDiv”, regardless of where they are inside the div element

Select elements based on their parent

$("div#displayDiv > p")

Selects all paragraph elements that are children of the <div> whose ID is “displayDiv”

Select the first child of a parent

$("ul#displayList > li:first")

Selects the first list item element in the unordered list whose ID is “displayList”

Select the last child of a parent

$("ul#displayList > li:last")

Selects the last list item element in the unordered list whose ID is “displayList”

Select elements by attribute

$("input[name='firstName']")

Selects the input element whose name attribute is “firstName”

Once you understand the common selection operations, you can move ahead to combine them to create more sophisticated selectors. The jQuery library also supports a number of extensions that provide yet more capabilities.

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