jquery links inside child div

Go To StackoverFlow.com

0

Im having a problem with a jquery link inside a div

To explain, I have a <div id=map></div> which inside that loads a separate page (inc_map.php) of a map.

Outside of this div, on the main page, I have 4 links that control the character inside the div by reloading it. When you click one of them, a function runs, like:

$(function() {
  $("#left").click(function(evt) {
     $("#map").load("inc_map.php?move=left");
     evt.preventDefault();
     })

I want to change the links from outside of the div, to inside of the div, but it's causing me grief and not working.

Any idea why? I've tried naming them different id's etc too.

2012-04-04 17:38
by user1022585
Your're replacing the content in #map when loading new content, so the links will disappear if added to #map before the load takes place. Then again a little more info than "causing grief" would make it easier to know what exactly the problem is - adeneo 2012-04-04 17:57


0

please try this: (example div#map)

HTML

<div id="map">
    <a href="#" id="left">L1</a>
    <a href="#" id="right">L2</a>
    <a href="#" id="top">L3</a>
    <a href="#" id="bottom">L4</a>
</div>

jQuery

$(function() {
    $('#left').on('click', function(evt) {
        var $contents = $('#map').contents();
        $("#map").load("inc_map.php?move=left", function() {
            $(this).prepend($contents);
        });
        evt.preventDefault();
    });
});
2012-04-04 18:16
by thecodeparadox
What exactly is the self variable for - adeneo 2012-04-04 18:20
thanks, self is no longer necessary. it works? - thecodeparadox 2012-04-04 18:21


0

This works as expected for me:

<script type="text/javascript" language="javascript">
    $(function () {
        $("#left").click(function (evt) {
            $("#map").load("test.html?move=left");
            evt.preventDefault();
        });
    });


</script>

<div id="map">   <a id="left">TEST CLICK </a>
</div>

Maybe you can try debugging it with firebug to see the whole picture. Hope it helps.

2012-04-04 18:14
by Ernesto
Would it still work if the data in the div was filled using a load(page.php) command, and the 'left' id was on that page? Should that effect parent divs or nah - user1022585 2012-04-04 18:16
I would think it won't affect how it works the first time. The second time wont work with the link inside the new content. You would have to bind the click event again. At least in my example the click event is bound to the control with the left id at the beginning, so a new call to the function would be neccessary - Ernesto 2012-04-05 14:09
Ads