Search code examples
javascriptjqueryapijquery-selectors

Insert html using jquery .html()


I want to insert a large chunk of html into a pre-existing <td>. I am using this method:

 $("td#content").html(LOTS_OF_HTML_CODE_HERE);

But it doesn't work. I am a noob, there are a lot of quotes and ' within the HTML chunk that seems to be breaking it. What is the correct method for doing this?


Solution

  • I would suggest unifying the html into one string... like so.

    htmlStr = "";
    htmlStr += "<p>some paragraph";
    htmlStr += "<p>another paragaraph</p>";
    
    $("#content").html(htmlStr);
    

    this way you can see where your html is breaking down and it adds a lot of readability to javascript created content.

    Also...

    if there is content in this TD that you'd like to keep, I would use the append() jquery method.


    the jquery documentation is your best friend!