0

I have this code :

<!DOCTYPE html>
<body>
    <div id=div1 style="display:block;">
        <p id="intro">Hello World!</p>
        <p>This example demonstrates the <b>getElementById</b> method!</p>
    </div>
    <div id=div2 style="display:none;">
        <p id="intro">Hello World again!</p>
        <p>This example demonstrates the <b>getElementById</b> method!</p>
    </div>
    <button type="button" onclick="func()">Change div</button>
    <script>
        function func() {
            x = document.getElementById("div1");
            x.style.display = "none";
            x1 = document.getElementById("div2");
            x1.style.display = "block";
        }
    </script>
</body>

It basically displays div2 on button click.

This works fine when div2 and div1 are in same file. I want div2 to be in different file and be displayed when we click the button without changing the url.

One way to achieve this is by using iframes and changing the url(in which div2 is there) of iframe on button click.

Is there any other way to achieve this?

I am very new to html and javascript so an explation with answer will help a lot. Thanks!

3
  • Use PHP! de.wikipedia.org/wiki/PHP Commented Jun 18, 2013 at 14:15
  • You can used jQuery load api.jquery.com/load Commented Jun 18, 2013 at 14:17
  • @algorhythm can it be done only with PHP without using jQuery or AJAX, as other have suggested if yes than a small code on how to achieve that would help. I know some basic php but I am completely unaware of AJAX and jQuery. If this can be done only by using PHP than I would like to take that approach. Thanks! Commented Jun 18, 2013 at 14:28

2 Answers 2

2

For these type of things that you want to happen dynamically on your page without getting the whole page to load you need to use AJAX.

And this code below may help you understanding some very basic technique of using AJAX thorugh jQuery:

$(document).ready(function(){
    $("button").click(function(){
        alert('Load was performed.');
        $.get('target_file.html', function(data) {
            $('div').html(data);
            alert('Load was performed.');
        });
    });
});

To be more specific, you can find that <div> from your target file like this:

$("div").html(data.find("#div_id"));

One more thing. If you want to use PHP you can see this post here :

php: Get html source code with cURL

Sign up to request clarification or add additional context in comments.

3 Comments

how stackoverflow.com/questions/3592270/… solves my problem kindly elaborate. Thanks!
This tells you how to get a source code of a file and then you would be able to print out that div on the button click.
I have to try it out before accepting the answer. I will try to learn about curl. I will try to post the code and then accept your answer but it might take few days. Upvoted it though. Thanks for your guidance.
0

You need to use AJAX to do so if you do not want to work with iframes. See http://en.wikipedia.org/wiki/Ajax_(programming)

Comments

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.