0

I have dropdown on page with this menu item:

<li><a onclick="return false" href="javascript:void(0)" class="change-state" data-id="@Model.Id" data-state="@ReportStatus.New">Mark as New</a></li>

And I have jquery event handler for click on this <a>

$(function () {
     $('.change-state').click(function () {
         var attr = this.dataset;
         $.ajax({
             method: 'POST',
             url: '@Url.Action("ChangeReportStatus", "Default")',
             data: { id: attr.id, status: attr.state },
             success: function (data) {
                 var name = '#report-number-' + attr.id;
                 $(name).html(data);
             }
         });
     });
});

And when I open my page, click this <a> I have changes by ajax, but if I try to click it again, nothing works. I think it's because I using javascript:void(0), but without this my ajax refresh all page, not only element. Please help me to solve this issue.

1 Answers1

0

From your code I am assuming that you are getting the name of a container and then replacing the html in it which includes you anchor tag? If so then a click event won't do it because the anchor tag will be dynamically created again and it won't be registered to use your click event. You need to try this as your event handler instead

$('.change-state').on('click', function() {
   ....
});
Gjohn
  • 1,271
  • 1
  • 8
  • 12