4

I am displaying multiple dynamic links which use the same , ajaxloads the content on the first link fine but doesn't work for the rest of them. How can I get it to load the content of other links in 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);
            }
        });
    }
    });
3
  • Try using success instead of complete. Or have you already tried that? Commented Jul 7, 2016 at 3:56
  • I tried it, when i use success it doesn't even load the content of one link that is working Commented Jul 7, 2016 at 3:58
  • Strange... what is the value of event.responseText? (with complete, not with success) Commented Jul 7, 2016 at 4:01

1 Answer 1

3

Change the id to a class, remove # from the 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);
            }
        });
    }
    });
Sign up to request clarification or add additional context in comments.

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.