I have a fundamental confusion about how jQuery, and probably javascript work. I keep having the same issue, how to pass parameters to jQuery functions so that their methods in turn can call functions using those variables. Here is my most recent example:
I am using fullcalendar plugin. If I click on the calendar, it fires a callback method 'select'. The select callback is automatically given certain parameters: 'startDate' 'endDate', etc. What I want is to open a jQuery dialog to gather additional information and then post the new event to the server. Along the lines of this:
$('calendar').fullcalendar({
...
select: function (startDate, endDate) {
$('#newEventPopup').dialog('open');
in the html I have something like this:
<div title = 'How cool is this event?' id='newEventPopup'>
<select id='coolness'>
<option value = 'super'>Super!</option>
<option value = 'cool'>Cool</option>
<option value = 'dorky'>Dorky</option>
<option value = 'lame'>Lame!</option>
</select>
</div>
finally, I would like to add a button to the dialog to post the fullcalendar variables as well as the new variable to the server:
var coolness = $('#coolness');
$('#newEventPopup').dialog({
modal: true,
autoOpen: false,
...
button: {
Save : function (){
$.ajax({
url: 'sample.php'
type: 'POST'
data: {
'start' : startDate,
'end' : endDate,
'coolness' : coolness
}
success: $('calendar').fullCalendar('refetchEvents')
}
}
}
});
I simply don't understand how to build this, or where to place the code so that the dialog 'save' button 'knows' what the variables 'startDate' 'endDate' and 'coolness' all mean. I should mention that I am originally a Java programmer, and I am still struggling with JavaScript function based variable scope.
I have had this problem with many such jQuery methods where I want one method to point to some external function (like the select callback method invoking $.dialog) which in turn executes another method (like like the button callback method invoking the $.ajax function) but how do you pass parameters to $.ajax or $.dialog so their own methods can use those values?
Thanks,