arslan2037's avatar

How to append html file in javascript

I want to append html onclick and i know how to do it, but i want to add file path instead of html code in append method, here is my code

    <!DOCTYPE html>
    <html>
            <head>
                <title>Home</title>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
            <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
                <script type="text/javascript">
                        $(function () {
                            $.get("profile.html", function (data) {
                                    $("#appendToThis").append(data);
                             });
                        });
                </script>
            </head>
        <body>
        <div id="appendToThis"></div>
    </body>
    </html> 
0 likes
21 replies
tomopongrac's avatar
Level 51

Try with html method

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

https://api.jquery.com/jquery.get/

You can check do you get any result with

<!DOCTYPE html>
    <html>
            <head>
                <title>Home</title>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
            <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
                <script type="text/javascript">
                        $(function () {
                            $.get("profile.html", function (data) {
                    alert(data); // <-- add this code
                                    $("#appendToThis").html(data);
                             });
                        });
                </script>
            </head>
        <body>
        <div id="appendToThis"></div>
    </body>
    </html> 
arslan2037's avatar

absolute link? not know about it, please help :(

arslan2037's avatar

give it a complete path but not working

    $.get( "C:\Users\Arslan\Desktop\JQuery-Practice\profile.html", function( data ) {
    $( ".result" ).html( data );
    alert( "Load was performed." );
    });
tomopongrac's avatar

i mean that you need to put your web link ... don't you have some local server on which you execute your script

arslan2037's avatar

thanks for help, now its working, its a problem of link

arslan2037's avatar

right now its importing it one time, can i do it multiple time? like on click event

arslan2037's avatar

my code is like this now,

    $(document).ready(function(){
            var max_fields = 10; //maximum input boxes allowed
            var x = 1; //initlal text box count

            if (x < max_fields) {
                x++;
                $('.button').click(function(){
                    $.get('profile.html', function(result){
                        $('.import').html(result);
                    });
                });
            }
        });

but its importing it only one time

tomopongrac's avatar

what exactly do you want ... now on click you get result into tag with classname import

do you want to append ... than you change html method with append

$(document).ready(function(){
            var max_fields = 10; //maximum input boxes allowed
            var x = 1; //initlal text box count

            if (x < max_fields) {
                x++;
                $('.button').click(function(){
                    $.get('profile.html', function(result){
                        $('.import').append(result);
                    });
                });
            }
        });
1 like
arslan2037's avatar

i want how many time user click this button, this file import here that many times

arslan2037's avatar

.append works for me :) , only one thing left in it

arslan2037's avatar

can we add copy and delete button also on each import?

arslan2037's avatar

clone can copy all the data in input fields from previous element to current element?

Please or to participate in this conversation.