This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Prevent execution of parent event handler
I need to attach functions to onclick events of hierarchical divs.
I have this HTML
<div onclick="event1()" class="wrapper">
main contents
<div onclick="event2()"class="inner">
inner contents
</div>
</div>
now when i click on inner div event1() is being called, and event2() is not being called because I think my jquery plugin blocks it.
Edited ::
actually my plugin blocks the child node events so event2() is never being called how can i stop that ?
I am using jquery full callender plugin : http://arshaw.com/fullcalendar/
and below is my configuration function which is being called on onready.
function calenderEvents(events, account_id) {
//Dynamically Set options as account type wise
var selectable_opt = '';
if (account_id == 'default') {
selectable_opt = true;
} else {
selectable_opt = false;
}
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var calendar = $('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
selectable: selectable_opt,
selectHelper: true,
eventDrop: function(event, dayDelta, minuteDelta, allDay, revertFunc) {
AfterMove(event);
},
select: function(start, end, allDay) {
var title = prompt('Event Title:');
if (title) {
var details = {
title: title,
start: start,
end: end,
allDay: allDay
};
$.post(SITE_URL + '/calendar/add-event', {
details: details
}, function() {
});
calendar.fullCalendar('renderEvent', {
title: title,
start: start,
end: end,
allDay: allDay,
}, true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
/*eventMouseover: function() {
$('.fc-event-delete').css('display','block');
},
eventMouseout: function() {
$('.fc-event-delete').css('display','none');
},*/
editable: true,
events: events,
});
//}).limitEvents(2);
}
You can add the event handler to the container element and supply a selector so only events triggered by elements that match that selector will invoke the handler. Because the handler is being attached to the containing element, child elements that are added to the DOM later will still invoke the handler, if they match the selector.
http://api.jquery.com/on/
This code will create an event handler that will be triggered on new elements that are added to the div#wrapper element. The #adder click handler will add new elements to the wrapper.
HTML
<div id="adder">click to add elements</div>
<div class="wrapper">
contents:
<div class="inner">0</div>
</div>
JS
var $inner = $('.inner').first(),
$wrapper = $('.wrapper'),
count = 0;
$wrapper.on('click', '.inner', function(e) {
alert('click from ' + $(this).text());
});
$('#adder').on('click', function() {
$wrapper.append($inner.clone().text(++count));
});
The main thing is the use of the .inner selector when the click event handler is added to $wrapper.
Shown in this jsFiddle.
You need to stop the event being propagated to the parent.
Use event.stopPropagation();
$(".inner").click(function(event){
//do something
event.stopPropagation();
});
This effect is call event propagation.
Inner div click handler has to be just like this to prevent propagation:
var event2 = function(event) {
event = event || window.event;
if (event.stopPropagation) {
// for adequate browsers
event.stopPropagation()
} else {
// for IE
event.cancelBubble = true
}
}
demo - http://jsfiddle.net/Qw92P/
Just use one click event on the wrapper but make it "live". Detect if the click was actually on the child using targetElement (or is it srcElement--you can look up this part).
Related
I saw a CodePen that allowed dragging an event from FullCalendar to trash or back to an external list. I forked the CodePen: https://codepen.io/hlim18/pen/EMJWQP?editors=1111. The JavaScript part of the working code with jQuery is the following:
$(document).ready(function() {
/* initialize the external events
-----------------------------------------------------------------*/
$('#external-events .fc-event').each(function() {
// store data so the calendar knows to render an event upon drop
$(this).data('event', {
title: $.trim($(this).text()), // use the element's text as the event title
stick: true // maintain when user navigates (see docs on the renderEvent method)
});
// make the event draggable using jQuery UI
$(this).draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});
});
/* initialize the calendar
-----------------------------------------------------------------*/
$('#calendarSchedule').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar
dragRevertDuration: 0,
drop: function() {
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
console.log(this);
$(this).remove();
}
},
eventDragStop: function( event, jsEvent, ui, view ) {
if(isEventOverDiv(jsEvent.clientX, jsEvent.clientY)) {
$('#calendar').fullCalendar('removeEvents', event._id);
var el = $( "<div class='fc-event'>" ).appendTo( '#external-events-listing' ).text( event.title );
el.draggable({
zIndex: 999,
revert: true,
revertDuration: 0
});
el.data('event', { title: event.title, id :event.id, stick: true });
}
}
});
var isEventOverDiv = function(x, y) {
var external_events = $( '#external-events' );
var offset = external_events.offset();
offset.right = external_events.width() + offset.left;
offset.bottom = external_events.height() + offset.top;
// Compare
if (x >= offset.left
&& y >= offset.top
&& x <= offset.right
&& y <= offset .bottom) { return true; }
return false;
}
});
I would like to write the code without using jQuery. So, I tried to change jQuery to vanilla JavaScript. But, the calendar is not even displayed in the screen.
This is how I tried: https://codepen.io/hlim18/pen/bZyaQj?editors=1111.
The JavaScript part with vanilla JavaScript I tried is the following:
/* initialize the external events
-----------------------------------------------------------------*/
var draggable_events = document.querySelectorAll('#external-events .fc-event');
for(var i=0; i<draggable_events.length; i++){
// store data so the calendar knows to render an event upon drop
draggable_events[i].fullCalendar('renderEvent', {
title: draggable_events[i].innerText, // use the element's text as the event title
stick: true // maintain when user navigates (see docs on the renderEvent method)
});
// make the event draggable
draggable_events[i].draggable({
zIndex: 999,
revert: true, // will cause the event to go back to its
revertDuration: 0 // original position after the drag
});
}
/* initialize the calendar
-----------------------------------------------------------------*/
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendarSchedule');
var calendar = new FullCalendar.Calendar(calendarEl, {
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar
dragRevertDuration: 0,
drop: function() {
// is the "remove after drop" checkbox checked?
if (document.getElementById('drop-remove').checked = true) {
// if so, remove the element from the "Draggable Events" list
this.remove();
}
},
eventDragStop: function( event, jsEvent, ui, view ) {
if(isEventOverDiv(jsEvent.clientX, jsEvent.clientY)) {
calendarEl.fullCalendar('removeEvents', event._id);
var el = document.querySelector('fc-event').setAttribute("id", "external-events-listing").text( event.title );
el.draggable({
zIndex: 999,
revert: true,
revertDuration: 0
});
el.data('event', { title: event.title, id :event.id, stick: true });
}
}
}
});
var isEventOverDiv = function(x, y) {
var external_events = document.getElementById('external-events');
var offset = external_events.offset();
offset.right = external_events.width() + offset.left;
offset.bottom = external_events.height() + offset.top;
// Compare
if (x >= offset.left && y >= offset.top && x <= offset.right && y <= offset .bottom){
return true;
}
return false;
}
In the CodePen, I don't see any errors. But, when I test with my app I'm working on, I see the following error:
Uncaught TypeError: draggable_events[i].fullCalendar is not a function
at viewMonthly.js:15
I'm not even sure how many problems are there in total to solve to make the code work with vanilla JavaScript... :(
So, I'd appreciate any advice. Thank you in advance! :)
fullCalendar in versions prior to V4 is a jquery plugin and needs jquery to work
I want to manually input the time in the modal , however everytime the modal pops up the time input field has already a value which is the current time in my computer's clock.
How do I edit the javascript so that it will not generate the time automatically.
I used the plug in here https://github.com/tliokos/jquery-fullcalendar-crud . I just Downloaded it. By the way it is my first time to use this plug in.
This is the javascript code:
$(function(){
var currentDate; // Holds the day clicked when adding a new event
var currentEvent; // Holds the event object when editing an event
$('#color').colorpicker(); // Colopicker
$('#time').timepicker({
minuteStep: 5,
showInputs: false,
disableFocus: true,
showMeridian: false
}); // Timepicker
// Fullcalendar
$('#calendar').fullCalendar({
timeFormat: 'H(:mm)',
header: {
left: 'prev, next, today',
center: 'title',
right: 'month, basicWeek, basicDay'
},
// Get all events stored in database
events: 'crud/getEvents.php',
// Handle Day Click
dayClick: function(date, event, view) {
currentDate = date.format();
// Open modal to add event
modal({
// Available buttons when adding
buttons: {
add: {
id: 'add-event', // Buttons id
css: 'btn-success', // Buttons class
label: 'Add' // Buttons label
}
},
title: 'Add Event (' + date.format() + ')' // Modal title
});
},
// Event Mouseover
eventMouseover: function(calEvent, jsEvent, view){
var tooltip = '<div class="event-tooltip">' + calEvent.description + '</div>';
$("body").append(tooltip);
$(this).mouseover(function(e) {
$(this).css('z-index', 10000);
$('.event-tooltip').fadeIn('500');
$('.event-tooltip').fadeTo('10', 1.9);
}).mousemove(function(e) {
$('.event-tooltip').css('top', e.pageY + 10);
$('.event-tooltip').css('left', e.pageX + 20);
});
},
eventMouseout: function(calEvent, jsEvent) {
$(this).css('z-index', 8);
$('.event-tooltip').remove();
},
// Handle Existing Event Click
eventClick: function(calEvent, jsEvent, view) {
// Set currentEvent variable according to the event clicked in the calendar
currentEvent = calEvent;
// Open modal to edit or delete event
modal({
// Available buttons when editing
buttons: {
delete: {
id: 'delete-event',
css: 'btn-danger',
label: 'Delete'
},
update: {
id: 'update-event',
css: 'btn-success',
label: 'Update'
}
},
title: 'Edit Event "' + calEvent.title + '"',
event: calEvent
});
}
});
// Prepares the modal window according to data passed
Suggest looking at the docs for the timepicker.
https://jdewit.github.io/bootstrap-timepicker/
In which case set defaultTime=false
defaultTime
'current' (default) - Set to the current time.
Set to a specific time - '11:45 AM'
false - Do not set a default time
I´m using the full calendar plugin, so I load my data for the current month, but when I click previous, then next then previous again; my previous events get duplicated.
For example, if I had an event with id=1 in november, and I´m in december and I go to november; then I see my event with id=1 duplicated.
Here is my json:
[{"id":2024,"title":"titulo0","start":"2014-12-23 19:22:17","end":"2014-12-23 19:22:17","description":"descripcion0"}]
Here is how I load my calendar:
function loadEventos() {
var current_url = '';
var new_url = '';
$('#calendar').fullCalendar({
events: '/pushCombos/calendarioAction!getEventosMes.action?mes=' + 1,
header: {
left: 'prev,next today',
center: 'title',
right: ''
},
editable: true,
droppable: true, // this allows things to be dropped onto the calendar
drop: function() {
// is the "remove after drop" checkbox checked?
if ($('#drop-remove').is(':checked')) {
// if so, remove the element from the "Draggable Events" list
$(this).remove();
}
},
eventClick: function(calEvent, jsEvent, view) {
calEvent.title = "CLICKED!";
console.log(moment(calEvent.end).format("YYYY-MM-DD"));
editarEvento(calEvent);
console.log(moment(calEvent.end).format("YYYY-MM-DD"));
$('#calendar').fullCalendar('updateEvent', calEvent);
}
});
}
and here is the code of my buttons:
$('.fc-button-group').children().eq(1).click(function(){
var date = $("#calendar").fullCalendar('getDate');
var month_int = moment(date).format("MM");
var year_int = moment(date).format("YYYY");
var events = {
url: '/pushCombos/calendarioAction!getEventosMes.action?mes=1',
data: {
month: month_int,
year: year_int
}
};
$('#calendar').fullCalendar('removeEventSource', events);
$('#calendar').fullCalendar('addEventSource', events);
});
When I edit an event and reload it; it reloads fine, but if I use the next and previous buttons, the events get duplicated.
Why is this happening??
Thanks in advance!
Try changing your events option to this:
events: {
url: '/pushCombos/calendarioAction!getEventosMes.action',
type: 'GET',
data: {
mes: 1
},
cache: false,
error: function (jqXHR, textStatus, errorThrow) { //whatever you want }
}
It seems that this plugin automatically does the post when you press the previous button that why it was duplicating the events.
Another weird thing I found is that the first day of the month and the last day of the month it sends to the server are wrong.
I am using the Full Calender js plugin, so far so good. but i want to check if a selection between the start and end has events?
I just need a true or false returned. Basically i want to stop users from creating events if an even already exists on the date selection.
var calendar = $('#calendar').fullCalendar({
selectable: true,
selectHelper: true,
firstDay: 5,
weekNumbers: false,
select: function (start, end, allDay, event) {
var TitleSet = false;
StartDate = start;
EndDate = end;
if (event) {}
if (TitleSet) {
calendar.fullCalendar('renderEvent', {
title: title,
start: start,
end: end,
allDay: allDay
},
true // make the event "stick"
);
}
calendar.fullCalendar('unselect');
},
editable: true,
events: EventsArr,
eventRender: function (event, element) {
element.qtip({
content: event.description
});
}
});
I tried this methode and it looks fine
// check if this day has an event before
function IsDateHasEvent(date) {
var allEvents = [];
allEvents = $('#calendar').fullCalendar('clientEvents');
var event = $.grep(allEvents, function (v) {
return v.start === date;
});
return event.length > 0;
}
then you can call it from dayclick event
dayClick: function (date, allDay, jsEvent, view) {
if (!IsDateHasEvent(date)) {
selectedDate = date;
$("#divAddNewAppointment").dialog("open");
}
else {
$('<%= "#" + lblMessage.ClientID%>').html(" your error msg");
$("#divMessage").dialog("open");
}
}
You will need to create a method to get the array of events already loaded in calendar, after that use that array to get all days that have events and if user clicks on one of that dates don´t let create another event. This is something that only you can make and figure out, you will need to make an algorithm for yourself...
Check this clientEvents
I'm a js noob. I'm using the jquery FullCalendar plugin which exposes an eventClick(calEvent, jsEvent, view) event which is called when you click on an event on the calendar. In my event handler I want to pop up a dialog asking if the user wants to edit just one occurrence of a recurring event or the whole event. The problem is that the button click handlers for THAT dialog need to have access to the calEvent variable, but I have to instantiate the dialog inside $(document).ready but outside of the eventClick function.
Here's my code so far:
$(document).ready(function() {
$('#calendar').fullCalendar({
events: getEvents,
header: {
left: 'title',
center: 'prev,next',
right: 'today month agendaWeek agendaDay'
},
theme: true,
eventClick: function(calEvent, jsEvent, view) {
if(calEvent.readOnly == true) {
return;
}
if(calEvent.recurring) {
if(calEvent.persisted) {
editOccurrence(calEvent);
} else {
$("#edit_type_dialog").dialog("open");
}
} else {
editEvent(calEvent);
}
}
});
$("#edit_type_dialog").dialog({
modal:true,
autoOpen: false,
buttons: {
All:function() {
$(this).dialog("close");
editEvent(calEvent);
},
This:function() {
$(this).dialog("close");
editOccurrence(calEvent);
},
Cancel:function() {
$(this).dialog("close");
}
}
});
function getEvents(start, end, callback) {
//get some events
}
function editEvent(calEvent) {
alert("event edited");
}
function editOccurrence(calEvent) {
alert("occurrence edited");
}
});
So the question boils down to: how do I get the calEvent over to the button click event handlers in the edit_type_dialog from the eventClick function?
Here's what I did (not sure if it's recommendable, but it works):
1) Move the dialog instantiation into the eventClick method, thereby giving it access to the calEvent variable.
2) Outside the fullCalendar instantiation, but still within $(document).ready() I added $("#edit_type_dialog").hide()