Insert a <span> element at the end of each <p> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/* w w w . ja v a2 s. c o m*/ <script> $(document).ready(function(){ $("button").click(function(){ $("<span>Hello World!</span>").appendTo("p"); }); }); </script> </head> <body> <button>test</button> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </body> </html>
The appendTo()
method inserts HTML elements at the end of the selected elements.
To insert HTML elements at the beginning of the selected elements, use the prependTo()
method.
$(content).appendTo(selector)
Parameter | Optional | Description |
---|---|---|
content | Required. | the content to insert. If content is an existing element, it will be moved from its current position. |
selector | Required. | on which elements to append the content to |