views:

46

answers:

1

What's the best way to target a particular <div> with a selection from a menu using php?

Specifically, I want to have a link selected from a menu contained in div id=menu loaded in div id=content.

I can see this has been discussed in here, but I can't seem to find a clear answer. So, my apologies if this is rehashing an old topic, but I gather that I need to use ajax for this, or can it be done with php only? Are there any good examples or tutorials available?

Thanks

+1  A: 

You'll probably need AJAX and a bit of magic.

HTML

<div id="menu"><a href="page.html">Page 1</a> | <a href="page2.html">Page 2</a></div>
<div id="content"></div>

JavaScript (with jQuery)

$("#menu a").each(function(e){ 
    $(this).bind("click", function(event){ 
        $("#content").load($(event.target).attr("href"));
    });
});

I haven't tested this, but it should work. Basically you want to bind to all the links within #menu and when one is clucked load the href into #content via an AJAX call.

Josh K