programming4us
programming4us
WEBSITE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us