capture click event from external source (child within parent) using jquery

I am using massrelevances polling feature. I include an embed code (below) which injects html into the webpage.

embed code from third-party

<div class="mr-space" data-space-id="project-id"></div>
<script src="//platform.massrelevance.com/js/massrel.js"></script>
<script>
  massrel.ui.load();
</script>

I want to capture a click event from the html that is injected from their embed code.

The only clicks it will register is when I set it to look within the body, which isn't practical, I need it to register a click within div.option-buttons

var link = {};

link.init = function() {
    $('.option-buttons').contents().on('click', function () {
      alert('clicked');
    });
}

window.onload = link.init;

html that is generated from the embed code above

<div class="option-buttons">
    <a href="">Vote</a>
</div>  

Answers


Its not clear but you can try

    $(document).on('click', '.option-buttons a', function () {
      alert('clicked');
    });

EDIT

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the event binding call.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.

You need to use Event Delegation. You have to use .on() using delegated-events approach.

General Syntax

$(document).on(event, selector, eventHandler);

Ideally you should replace document with closest static container.


Need Your Help

onDrag called for each RecyclerView.ViewHolder

android android-recyclerview drag

My onDrag method seems to be called for each RecyclerView.ViewHolder that exists in RecyclerView. Does anyone know how to prevent this and make sure it is only called once?

re. Android) run an app to AndoridStudio or when you tried to install the apk app files are not visible

android facebook icons manifest

After Facebook Login connect, Run app in AndroidStudio, program execution is going with device, but the file will are not be visible. I do not know why this is the case happens.