1

I'm trying to create a website where HTML-code gets loaded into a div when document is done loading. Though, the hover function doesn't work with AJAX-load. Please help.

$(document).ready(function() {
    $('#sidebar').load('sidebar-main.php');

    $("#sidebar .section").hover(function() {
            $('#sidebar .section .left-mark').show();
        },function(){
            $('#sidebar .section .left-mark').hide();
        });
});

The "sidebar-main.php" file:

<div class='section' onclick='header()'><div class='left-mark'></div></div>

The sidebar in the main PHP-file:

<div id='sidebar'></div>
3
  • have you tried using on handler for ajax like $("#sidebar .section").on('hover',function(){ ...}); Commented Sep 23, 2017 at 20:01
  • Did not work :( Commented Sep 23, 2017 at 20:03
  • load() has a callback function when the load has completed. This means, I think, it works asynchronous. So I advice to use the callback function. See: api.jquery.com/load Commented Sep 23, 2017 at 20:05

3 Answers 3

2

On newly created elements (refer to .load()), you need to delegate the event handler to your #sidebar ancestor.

Hence, change this:

$("#sidebar .section").hover(function() {
    $('#sidebar .section .left-mark').show();
},function(){
    $('#sidebar .section .left-mark').hide();
});

to:

$("#sidebar").on('mouseenter', '.section', function() {
    $('#sidebar .section .left-mark').show();
}).on('mouseleave', '.section',function(){
    $('#sidebar .section .left-mark').hide();
});

A demo:

//
// instead on next line, for testing I added the next one
//
//$('#sidebar').load('1.php');
$('#sidebar').append("<div class='section' onclick='header()'>aa<div class='left-mark'>bb</div></div>");

$("#sidebar").on('mouseenter', '.section', function() {
    $('#sidebar .section .left-mark').show();
}).on('mouseleave', '.section',function(){
    $('#sidebar .section .left-mark').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id='sidebar'></div>

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

Comments

0

try this

$('#sidebar').load('sidebar-main.php', bindHoverEvent);
function bindHoverEvent() {
    $("#sidebar .section").hover(function() {
       $('#sidebar .section .left-mark').show();
    }, function(){
        $('#sidebar .section .left-mark').hide();
    });
}

Comments

0

So the reason this isn't working is because .load() is asynchronous, meaning the code following the .load() call will execute before the content is loaded. There's two solutions here:

  1. Put your hover call in the callback handler like this:

    $('#sidebar').load('sidebar-main.php', function() {
         $("#sidebar .section").hover(function() {
            $('#sidebar .section .left-mark').show();
         },function(){
            $('#sidebar .section .left-mark').hide();
        });
    });
    
  2. Attach the hover listener to the body tag but only do a call when #sidebar .section is the element hovered. Note that hover is shorthand for mouseenter and mouseleave. Like this:

    $('body').on('mouseenter', '#sidebar .section', function() {
         $('#sidebar .section .left-mark').show();
    }).on('mouseleave', '#sidebar .section', function() {
         $('#sidebar .section .left-mark').hide();
    });
    

In the second solution you don't have to worry about the load being asynchronous because the listener is already attached to the body.

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.