0

I am trying to retrieve the data- attributes on button on HTML that was loaded from JQuery, I know this causes issues.

I can get the button click to register however I can not access any of the attributes.

$(document).on("click", ".reportRequest", function(e){
    console.log($(e.relatedTarget).data("ip"));
});

Here is the javascript and it returns undefined even though when I can see in inspect element that the button has assigned values

The HTML of the button is:

<button title="Report Request" class="btn btn-info pull-right reportRequest" data-toggle="modal" data-target="#shoutoutReportModal" data-ip="127.0.0.1" data-id="52"><i class="fa fa-flag" aria-hidden="true"></i></button>

I believe the issue is because the HTML is generated on page load with an AJAX request.

Any help will be appreciated

2
  • try with e.target instead of e.relatedTarget Commented Mar 3, 2018 at 15:31
  • From the jQuery docs: .relatedTarget -> "The other DOM element involved in the event, if any." - What other element should be involved in clicking a button? Commented Mar 3, 2018 at 15:32

1 Answer 1

3

Try by using $(this)

$(document).on("click", ".reportRequest", function(e){
    console.log($(this).data("ip"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you very much

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.