For complete documentation of jQuery manipulation methods, visithttp://api.jquery.com/category/manipulation/.
Get or set the text contents; HTML will be stripped.
$.fn.attrGet or set the value of the provided attribute.
$.fn.widthGet or set the width in pixels of the first element in the selection as an integer.
$.fn.heightGet or set the height in pixels of the first element in the selection as an integer.
$.fn.position $.fn.valGet or set the value of form elements.
There are a variety of ways to move elements around the DOM; generally, there are two approaches:
Place the selected element(s) relative to another element
Place an element relative to the selected element(s)
Example 3.27. Moving elements using different approaches
// make the first list item the last list item var $li = $('#myList li:first').appendTo('#myList'); // another approach to the same problem $('#myList').append($('#myList li:first')); // note that there's no way to access the // list item that we moved, as this returns // the list itself
Example 3.28. Making a copy of an element
// copy the first list item to the end of the list $('#myList li:first').clone().appendTo('#myList');
Note
If you need to copy related data and events, be sure to pass true
as an argument to $.fn.clone
.
Example 3.31. Getting a new element on to the page
var $myNewElement = $('<p>New element</p>'); $myNewElement.appendTo('#content'); $myNewElement.insertAfter('ul:last'); // this will remove the p from #content! $('ul').last().after($myNewElement.clone()); // clone the p so now we have 2
Note
var myItems = [], $myList = $('#myList'); for (var i=0; i<100; i++) { myItems.push('<li>item ' + i + '</li>'); } $myList.append(myItems.join(''));
Example 3.35. Using a function to determine an attribute's new value
$('#myDiv a:first').attr({ rel : 'super-special', href : function() { return '/new/' + $(this).attr('href'); } }); $('#myDiv a:first').attr('href', function() { return '/new/' + $(this).attr('href'); });
Copyright Rebecca Murphey, released under the Creative Commons Attribution-Share Alike 3.0 United States license.