Adding and removing HTML Elements
Dynamically adding elements to US HTML page is a powerful tool-it lets we modify not only the formatting, but the actual structure of our websites in response to a user ' s actions.
. Append () inserts the specified element as the last child of the target element. $ (". Info"). Append ("<p>Stuff!</p>");
. Prepend () inserts the specified elements as the first child of the target element. $ (". Info"). Prepend ("<p>Stuff!</p>");
. AppendTo () does the same as. Append (), $ (' <p>stuff!</p> '). AppendTo ('. Info ');
. Prepend () does the same as. Prepend ()
We can specify where in the DOM we insert a element with the . Before ( ) and . After () functions.
$ (' target '). After (' <tag>to add</tag> ');
var // existing elements // Move It // Same as:$ ("div"). After ($ ("P"));
Note:this does not copy the element from one location to another, it moves the original element effectively saving you fr Om has to delete the original
. Empty () deletes an element's content and all its descendants.
The . Remove () is deletes an element's content, but deletes the element itself.
modifying Classes and Content
. addclass () and. Removeclass () can used to add or remove a class from an element.
$ (' selector '). addclass (' className '); $ (' selector '). Removeclass (' ClassName ');
.toggleClass()
Function that is does exactly this. If the element it ' s called on Have the class it receives as an input, .toggleClass()
removes that class; if the target element doesn ' t has that class, .toggleClass()
adds
$("div").css("background-color","#008800"); //change css style
. HTML () and. Val can update contents of our HTML elements.
$ (' div '). html ("I love jquery!");
. val () is used to get the value of from elements.
$ (' input:checkbox:checked '). Val ();
Jquery-modifying HTML Elements