Search code examples
javascriptjqueryhtmljquery-data

Can't request data from div after ajax call. How do I fix this?


This example works well and the same I use on my project, but the data I use, in this case the div with the data-name attr is loaded by ajax and it looks like it doesn't work. In the console, firefox output this after the click:

getPreventDefault() sollte nicht mehr verwendet werden. Verwenden Sie stattdessen defaultPrevented.jquery.min...

http://jsfiddle.net/n495c/14/

js

      $(document).ready(function() {
        $("#content div").click(function() {
            var title = $(this).data("name");
            $("#content div").text(title);
        });
    });

html

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="content">
<div data-name="Neu"></div>
</div>

css

#content {
    width: 100%;
    height: 100%;
}

#content div {
    width: 50px;
    height: 50px;
    background: red;
    color: white;
}

Solution

  • Delegated event handler :

    $(document).ready(function() {
        $(document).on('click', '#content div', function() {
            var title = $(this).data("name");
            $("#content div").text(title);
        });
    });