var url = 'pollhandler.php'; var colors = ['red', 'green', 'blue', 'orange']; $(document).ready(function() { // Intercept form submissions $("form").submit(function() { return false; }); // Handle voting $('#castvote').click(poll_vote); /* Let the user request a new poll (use an anonymous function because poll_fetch takes an optional argument poll_id, and the callback will be passed the object that triggered the event) */ $('#newpoll').click(function () { poll_fetch(); }); }); /* poll_fetch(poll_id) * Fetches poll poll_id via Ajax * If no poll_id is specified, fetches a random poll */ function poll_fetch(poll_id) { $(this).attr('disabled', true).val("Getting new poll..."); if (!poll_id) { $.getJSON(url, null, poll_display); } else { $.getJSON(url, {'poll_id': poll_id}, poll_display); } } /* poll_display(data, textStatus) * Handler that displays a voting form for a poll retrieved by an Ajax call */ function poll_display(data, textStatus) { // Wrap jQuery functionality around the poll DOM element var poll = $("#poll"); // Fade the poll out poll.fadeOut(function() { // Empty the responses poll.find("#responses").empty(); // Update the question poll.find("#question").html(data.question); // Loop through the responses $.each(data.results, function(response_id, response) { // Create a div for this response var responseDiv = $('
').addClass('response'); // Create the input button /* Note: we have to specify the type of the input field when we create it, or else this won't work in IE. See this page for more info: http://msdn2.microsoft.com/en-us/library/ms534700.aspx */ var responseButton = $('').attr( {'name': 'vote', 'value': response_id, 'id': 'response-' + response_id} ).appendTo(responseDiv); // Create a form label with the response text var responseText = $('