javascript - How can I use the .parent() method to create a button that's specific to only one <div>? -
i'm using jquery plugin fullcalendar , want make 'weekends' button toggles weekends in calendar. made button have multiple calendar views on same page , button appears on of them , want appear on main calendar. so, thought use .parent() method calendar header class "calendar" have button. if think have better solution feel free share, here's code:
var $('.fc-header').parent() = parent if parent == ".calendar" $('.fc-header-left').append('<span class="weekends fc-button fc-button-today fc-state-default fc-corner-left fc-corner-right fc-state-active"><span class="fc-button-inner"><span id="weekends" class="fc-button-content">weekends</span><span class="fc-button-effect"><span></span></span></span></span>'); $("#weekends").click(function () { }); i'm sure there numerous issues code, great if describe of them, thanks!
ok, 1st thing, var declaration not valid, have backwards. can not assign variable name that. 2nd, if statement wrong. 3rd, $("#weekends").click() event not work on elements created dynamically. need use .on() 3 parameters, in order attach dynamically created elements. 4th, grab element want correct class , skip of others, since adding class="calendar" element want right? no need check of nonsense , grab parent(s).
better solution: select actual element instead of selecting elements of $('.fc-header') (2 lines of code, no if statement, , less confusion):
$(".calendar").children(".fc-header-left").append('<span class="weekends fc-button fc-button-today fc-state-default fc-corner-left fc-corner-right fc-state-active"><span class="fc-button-inner"><span id="weekends" class="fc-button-content">weekends</span><span class="fc-button-effect"><span></span></span></span></span>'); $(".calendar").on("click", ".fc-button-content", function() { // perform click code here... }); this code assumes there element child has class of fc-header-left immediate child of element calendar class. ofcourse, don't know how set dom since didn't post html, jquery, taking educated guess on this.
Comments
Post a Comment