Mihir
Mihir

Reputation: 8734

How to add huge html data through jquery to a div in html page

I have following html markup. I want to add this mark up to master page (or any other html page) through jquery. I don't want to include this html, in the body of that html page, because I dont want to touch my master page. I just want to add a div in the master page and want to append the following markup to that div. I want to append this html through the jquery. How to do it?

    <div id="mymenuDiv">
    <ul id="nav">
        <li class="top"><a href="#nogo1" class="top_link"><span>Home</span></a></li>
        <li class="top"><a href="#nogo2" id="products" class="top_link"><span class="down">Products</span></a>
            <ul class="sub">
                <li><a href="#nogo3" class="fly">Cameras</a>
                        <ul>
                            <li><a href="#nogo4">Nikon</a></li>
                            <li><a href="#nogo5">Minolta</a></li>
                            <li><a href="#nogo6">Pentax</a></li>
                        </ul>
                </li>
                <li class="mid"><a href="#nogo7" class="fly">Lenses</a>
                        <ul>
                            <li><a href="#nogo8">Wide Angle</a></li>
                            <li><a href="#nogo9">Standard</a></li>
                            <li><a href="#nogo10">Telephoto</a></li>
                            <li><a href="#nogo11" class="fly">Zoom</a>
                            </li>
                            <li><a href="#nogo15">Mirror</a></li>
                        </ul>
                </li>
                <li><a href="#nogo19">Flash Guns</a></li>
                <li><a href="#nogo20">Tripods</a></li>
                <li><a href="#nogo21">Filters</a></li>
            </ul>
        </li>
    </ul>
    </div>

Upvotes: 0

Views: 594

Answers (4)

Abhilash
Abhilash

Reputation: 1610

You can use jQuery $.ajax() for this, like so:

$.ajax({
    url: 'ajax/test.html',
    success: function(data) {
      $('#mydiv').html(data);
    }
});

To make this even simpler, there is $(selector).load() like so:

$('#mydiv').load('ajax/test.html')

You can take it a step further by specifying the id of the div tag from the ajax page. This will help if the ajax page is a full html page and you do not want to inject the html, head, et al tags in the body of your calling page. You can do it like this:

$('#mydiv').load('ajax/test.html #div_tag_to_load')

Upvotes: 0

jpumford
jpumford

Reputation: 548

The simplest way:

$('#myMenuDiv').html('<ul id="nav"><li class="top"><a href="#nogo1" class="top_link"><span>Home</span></a></li><li class="top"><a href="#nogo2" id="products" class="top_link"><span class="down">Products</span></a><ul class="sub"><li><a href="#nogo3" class="fly">Cameras</a><ul><li><a href="#nogo4">Nikon</a></li><li><a href="#nogo5">Minolta</a></li><li><a href="#nogo6">Pentax</a></li></ul></li><li class="mid"><a href="#nogo7" class="fly">Lenses</a><ul><li><a href="#nogo8">Wide Angle</a></li><li><a href="#nogo9">Standard</a></li><li><a href="#nogo10">Telephoto</a></li><li><a href="#nogo11" class="fly">Zoom</a></li><li><a href="#nogo15">Mirror</a></li></ul></li><li><a href="#nogo19">Flash Guns</a></li><li><a href="#nogo20">Tripods</a></li><li><a href="#nogo21">Filters</a></li></ul></li></ul>');

It's ugly.

Upvotes: 0

rajesh kakawat
rajesh kakawat

Reputation: 10896

try something like this

 $(function() {
   $(#master page div id).append($(#id of div from which you have to append));
 });

Upvotes: 0

user1726343
user1726343

Reputation:

$('body').append("<div id=\"mymenuDiv\">\r\n    <ul id=\"nav\">\r\n        <li class=\"top\"><a href=\"#nogo1\" class=\"top_link\"><span>Home<\/span><\/a><\/li>\r\n        <li class=\"top\"><a href=\"#nogo2\" id=\"products\" class=\"top_link\"><span class=\"down\">Products<\/span><\/a>\r\n            <ul class=\"sub\">\r\n                <li><a href=\"#nogo3\" class=\"fly\">Cameras<\/a>\r\n                        <ul>\r\n                            <li><a href=\"#nogo4\">Nikon<\/a><\/li>\r\n                            <li><a href=\"#nogo5\">Minolta<\/a><\/li>\r\n                            <li><a href=\"#nogo6\">Pentax<\/a><\/li>\r\n                        <\/ul>\r\n                <\/li>\r\n                <li class=\"mid\"><a href=\"#nogo7\" class=\"fly\">Lenses<\/a>\r\n                        <ul>\r\n                            <li><a href=\"#nogo8\">Wide Angle<\/a><\/li>\r\n                            <li><a href=\"#nogo9\">Standard<\/a><\/li>\r\n                            <li><a href=\"#nogo10\">Telephoto<\/a><\/li>\r\n                            <li><a href=\"#nogo11\" class=\"fly\">Zoom<\/a>\r\n                            <\/li>\r\n                            <li><a href=\"#nogo15\">Mirror<\/a><\/li>\r\n                        <\/ul>\r\n                <\/li>\r\n                <li><a href=\"#nogo19\">Flash Guns<\/a><\/li>\r\n                <li><a href=\"#nogo20\">Tripods<\/a><\/li>\r\n                <li><a href=\"#nogo21\">Filters<\/a><\/li>\r\n            <\/ul>\r\n        <\/li>\r\n    <\/ul>\r\n    <\/div>")

Better yet, stick it in an HTML file and do this:

$('body').load('myhtml.html');

Upvotes: 2

Related Questions