Open multiple dynamic links with the same ajax call.

I am showing a few dynamic links that use the same thing, ajax loads the content in the first link, but does not work for the rest. How can I load it to load the contents of other links into the same div?

Html:

$string .= '<a id="hrefid" data-id="'.$name["id"].'" href="link" >'.$name["name"].'</a>'.
<div id="content"> </div>

Jquery:

$('#hrefid').on('click', function (e) {
    var load = $(e.target).attr("href");
    if(load == "#link") {
        $.ajax({
            type: 'post',
            url: "/page/test/"+id,
            complete: function (event) {
                $("#content").contents().remove();
                $("#content").append(event.responseText);
            }
        });
    }
    });
+4
source share
1 answer

Change id to class, remove # from if statement

$string .= '<a class="hrefid" data-id="'.$name["id"].'" href="link" >'.$name["name"].'</a>'.
<div class="content"> </div>

$('.hrefid').on('click', function (e) {
    var el = $(this);
    var load = $(e.target).attr("href");
    if(load == "link") {
        $.ajax({
            type: 'post',
            url: "/page/test/"+id,
            success: function (event) {
                el.next().empty();
                el.next().append(event.responseText);
            }
        });
    }
    });
+3
source

All Articles