0

I'm trying to pass a function from .click() function but for some reason I'm not getting the value. Here is my code,

<script>
var guyid;

$(document).ready(function() {
    $('.guyid').click(function () {
        guyid = $(this).attr('id');  //This variable needs to pass
        alert(guyid);
    });
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicDay'
        },
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        eventSources: ['json-script.php?id=' + guyid]   //I need it here
    });
});

</script>

How can I pass the variable from .click() function to the eventSources? Need this help badly. Tnx.

3
  • 3
    You would need to re-run fullCalendar again with the changed property. That requires that plugin to support changing options after setup (most good plugins do). Commented Apr 13, 2015 at 8:39
  • As TrueBlueAussie says, when you setup fullCalendar, you bind eventSources with an array containing a string which is static (and contains the default guyid value which is null). You should run $yourCalendarInstance.refetchEvents() in the click event AFAIK. -> fullcalendar.io/docs/event_data/refetchEvents Commented Apr 13, 2015 at 8:43
  • I think you can move fullCalendar initialization inside click function block and trigger click manually Commented Apr 13, 2015 at 8:44

1 Answer 1

1

You need to destroy fullcalendar and re-initialize it.

Restores the element to the state before FullCalendar was initialized.

code

$(document).ready(function() {
    $('.guyid').click(function() {
        var guyid = $(this).attr('id'); 

        $('#calendar')
            .fullCalendar('destroy') //Destroy existing calendar
            .fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicDay'
                },
                editable: true,
                eventLimit: true, 
                eventSources: ['json-script.php?id=' + guyid] //Set updated id
            });

    });
});

OR, You can use events (as a function) with refetchEvents

var guyid = 'something';
$('#calendar').fullCalendar({
    events: function(start, end, timezone, callback) {
        $.ajax({
            url: 'json-script.php?id=' + guyid,
            dataType: 'JSON',
            success: function(doc) {
                var events = [];
                //Iterate are create 
                //This is pure hypothetical example
                $(doc).find('event').each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start') // will be parsed
                    });
                });
                callback(events);
            }
        });
    }
});

$('.guyid').click(function () {
    guyid = $(this).attr('id');  
    alert(guyid);

    $('#calendar').fullCalendar('refetchEvents');
});
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know fullCalendar, but maybe he allow to do such a thing like $('.guyid').click(function() { var guyid = $(this).attr('id'); $('#calendar') .fullCalendar({ eventSources: ['json-script.php?id=' + guyid] //I need it here }); }); });
@romuleald, It doesn't supports that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.