2

How do i load a test.html multiple time using jQuery.

function loadScreen()
{
  $('#result').load('test.html');
}

main.html

<div id='result'></div>
<input type="button" value="click me" onclick="loadScreen()">

test.html

<body>
<p>
testing
</p>
</body>

Current situation

when i click the button click me. it will load once show on main.html testing

what i want to do

How can i do it when i click the button twice it will show

testing
testing

and with different div id

1st time button click show testing <div id="result1">testing</div> 2nd time button click show testing <div id="result2">testing</div>

it will append incremental 1 at id.

2 Answers 2

5

load() will replace the content of the target element which is why it's not working as you want.

Try this instead:

function loadScreen() {
    var divId = $("#result > div").length + 1;
    $('#result').append("<div></div>").attr("id", "result" + divId).load('test.html');
}
Sign up to request clarification or add additional context in comments.

1 Comment

hmm i can't still unable to load test.html multiple times.
0

Then it's about time you should use $.get(), then create a div via the $(), add in the contents using .html() and .appendTo() the results div.

var result = $('#result'),            //cache result box
    i = 0;

function loadScreen(){
    $.get('test.html',function(data){ //initiate get
        $('<div>',{                   //create div for return data
            id : 'result'+(++i)       //add attributes
        })
        .html(data)                   //add data
        .appendTo(result);            //append to result
    });
}

1 Comment

hmm when i click the button 2nd time.nothing happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.