views:

96

answers:

2

On click a image how can another page be previewed in the same page without opening the actual page.And lets say the url /home/home.php?id=3
The preview should be in the preview div

 <img src="/home/app/img/toggle.gif" onlclick="show();" />
 <div id="preview"></div>   

Thanks..

+4  A: 

You can use .load() for this. Here's an example, HTML like this:

<img src="/home/app/img/toggle.gif" id="previewBtn" />
<div id="preview"></div>

jQuery like this:

$("#previewBtn").click(function() {
  $("#preview").load("/home/home.php?id=3");
});

Or, if you had lots of previews, something like this more generic works:

<a class="previewBtn" href="/home/home?id=1">Preview 1</a>
<a class="previewBtn" href="/home/home?id=2">Preview 2</a>
<div id="preview"></div>

With this jQuery:

$(".previewBtn").click(function() {
  $("#preview").load(this.href);
});
Nick Craver
And it will just open the link in preview mode rit?
Hulk
@Hulk yes - it will load the entire content of that page into the preview div, if you just want a portion, say the page's stuff that mattered was in a `<div id="content">` element, you can do this: `$("#preview").load("/home/home.php?id=3 #content");` and it'll load only that section.
Nick Craver
Oh cool.....Thanks Nick
Hulk
A: 
$(function() {

        $.ajax({
            url: '2.htm',
            success: function(data) {
                $('.result').html(data);
            }
        });
    });
XGreen
That's an awfully long way to write `$('.result').load('2.html')` :)
Nick Craver
awefully long but unit testable. they didnt design the success for nothing. its to get alerts about where your code is going wrong as you app gets bigger
XGreen
Thanks for letting us know..
Hulk
@XGreen - `.load()` also has a success callback, and status text, see for yourself: http://api.jquery.com/load/ :)
Nick Craver
well then it would be as long mate :)
XGreen