Widget code that allows for javascript event callbacks
<p>I'm creating a web application for submitting polls and whatnot. I have developed some widget code that, when a <code>&lt;div class = "desiredClass"&gt;</code> is present, a poll is fetched from the database via <code>ajax</code> request and is put into the html and users can vote on the poll. </p>

<p>so for my widget.js file that gets embedded into the html as a script:</p>

<pre><code>$(document).ready(function () {
  if ($('.loadPoll')[0]) {


    var name = $('.loadPoll').data("pollname");
    var IP_Address = '-1';
    $.ajax({
      url: 'http://url.com:3000/loadPoll',
      dataType: 'jsonp',
      data: {
        pollName: name
      },
      success: function (data) {
        poll = data.poll;
        $(".loadPoll").html(data.html);
        $(document).on('click', '#SubmitVote', function () {
          var selectedValue;
          var radios = document.getElementsByTagName('input');
          for (var i = 0; i &lt; radios.length; i++) {
            if (radios[i].type === 'radio' &amp;&amp; radios[i].checked) {
              selectedValue = radios[i].value;
            }
          }
          console.log(selectedValue);
          $.getJSON("http://jsonip.appspot.com?callback=?", function (data) {
            var IP_Address = data.ip;

            $.ajax({
              url: 'http://url.com:3000/submitVote',
              dataType: 'jsonp',
              data: {
                pollName: poll.pollName,
                voterID: IP_Address,
                vote: selectedValue
              },
              success: callback("Vote successful")

            });
          });
        });
      }
    });
  })
</code></pre>

<p>This basically sends a request to the server, html is returned, then, when <code>#SubmitVote</code> is 
clicked the value of the radio button is sent to the server as a vote.</p>

<p>I want to be able to "allow for javascript event callbacks". Pseudocode example: </p>

<pre><code>Poll.onSubmit(function() {
        console.log('hello world');
    });
</code></pre>

<p>How would one do this? </p>
