| Start |
|---|
| Creating DOM elements The You can optionally pass a second argument with element attributes:
the 'name':'value' pairs will appear between the |
| Manipulating element classes Assuming the page includes an HTML element like: jQuery provides useful functions to manipulate DOM classes, most notably Toggle a class Given the example markup, we can add a class with our first Now this would return
toggle Two classes: Boolean to add/remove classes: Function for class toggle (see example further down to avoid an issue) Used in examples: Examples: Use the element index to toggle classes odd/even More complex Simple functions for our examples: Add an odd/even class to elements with a If the row has a The classes that get returned are what is effected. Here, if an element does not have a |
| Other API Methods jQuery offers a variety of methods that can be used for DOM manipulation. The first is the .empty() (opens new window) method. Imagine the following markup: By calling Another handy function is the .closest() (opens new window) function: If you wanted to find the closest row to a button that was clicked within one of the row cells then you could do this: Since there will probably be multiple rows, each with their own If you wanted to get the It is usually considered good practise to prefix variables containing jQuery objects with a An alternative to and there is also a .parent() (opens new window) function as well:
|
| Sorting elements To sort elements efficiently (all at once and with minimal DOM interruption), we need to:
Make it cuteAdd a sort buttonSet the initial value of sorting direction Cache our DOM elements and |
| .html() You can use this method to replace all of the HTML within the selector. Assuming you have an html element like this You could use |
Read Article
https://develop.horje.com/learn/1434/reference