Sunday, 15 August 2010

Appending multiple html elements using Jquery -


New to jQuery and wondering if someone can advise me on best practice ...

I want to add a Div element to the page, I have a lot of HTML and is not sure how to achieve it .... Or if it is capable of using jquery ...

For example, if I want to add the following code on the page using jquery, then what is the best way.

  & lt; Div id = "test" & gt; & Lt; H1 & gt; This test is & lt; / H1> & Lt; P & gt; Hello, just a trial. / P & gt; & Lt; A href = "www.test.com" & gt; Click me & lt; / A & gt; & Lt; A href = "www.test.com" & gt; Click me again & lt; / A & gt; & Lt; / Div & gt;  

If you want to add HTML, then just use the jQuery app function . For example:

  $ ('body'). Attached ('

Change the selector from body to second element / selector according to your needs.

Or if you have div elements with ID "test" in the document, you can set the content using the html () function as follows:

  $ ("# Test"). Html ('& lt; h1 & gt; This test is & lt; / h1 & gt; \ & lt; p & gt; Hello, just a test & lt; / p & gt; \ & lt; a href = " Www.test.com "& gt; click me & lt; / a & gt; \ & lt; a href =" www.test.com "& gt; me again & lt; / a & gt; ');  

No comments:

Post a Comment