thanks for taking the time reading this.
I have a code snippet that has already some functions inside. Now I want to run that script if a button was clicked. From what I've tested it seems like it's not possible to put the snippet with its functions into a parent function (, is it?).
Now I wonder, what is the best way to call the javascript code snippet?
I don't think that is important for this general question, but here's my script I want to call:
var $input = $( '.datepicker2' ).pickadate({
})
var picker = $input.pickadate('picker')
var plusDays = 1;
var currentSelection = picker.get();
var currentDay;
var currentMonth;
var currentYear;
var newDay;
strich = currentSelection.indexOf("-");
currentYear = currentSelection.substring(0, strich);
strich = currentSelection.indexOf("-");
currentMonth = currentSelection.substring(strich + 1);
strich = currentMonth.indexOf("-");
currentMonth = currentMonth.substring(0, strich);
strich = currentSelection.indexOf("-");
currentDay = currentSelection.substring(strich + 1);
strich = currentDay.indexOf("-");
currentDay = currentDay.substring(strich + 1);
newDay = parseInt(currentDay) + plusDays;
var $input = $( '.datepicker' ).pickadate({
})
var picker = $input.pickadate('picker')
var date = new Date()
picker.set('select', [currentYear, currentMonth - 1, newDay])
Here you are:
function snippet() {
var $input = $( '.datepicker2' ).pickadate({});
var picker = $input.pickadate('picker')
var plusDays = 1;
var currentSelection = picker.get();
var currentDay;
var currentMonth;
var currentYear;
var newDay;
strich = currentSelection.indexOf("-");
currentYear = currentSelection.substring(0, strich);
strich = currentSelection.indexOf("-");
currentMonth = currentSelection.substring(strich + 1);
strich = currentMonth.indexOf("-");
currentMonth = currentMonth.substring(0, strich);
strich = currentSelection.indexOf("-");
currentDay = currentSelection.substring(strich + 1);
strich = currentDay.indexOf("-");
currentDay = currentDay.substring(strich + 1);
newDay = parseInt(currentDay) + plusDays;
var $input = $( '.datepicker' ).pickadate({
})
var picker = $input.pickadate('picker')
var date = new Date()
picker.set('select', [currentYear, currentMonth - 1, newDay])
}
And you can call everywhere, everytime you need:
$(document).ready(function() {
//stuff
snippet() ;
});
If you have a function inside the function (like this)
function snippet() {
var $input = $( '.datepicker2' ).pickadate({});
var picker = $input.pickadate('picker')
var plusDays = 1;
var currentSelection = picker.get();
var currentDay;
this.anotherFunction = function() {
// some stuff
}
}
You can call it:
snippet.anotherFunction();
Good luck
Related
I am having a problem with my JQuery code, after swal alert, it doesn't append value to the inputs.
What I do first I check if the value of the start_date is Empty if yes then swal alert, then when I select it the values won't append to the boxes...
function ChangeDate() {
debugger;
var Startdate = document.getElementById("lease_stattdate").value;
var thisDate = new Date(Startdate);
var month = thisDate.getMonth();
var year = thisDate.getFullYear();
var dateSelected = thisDate.getUTCDate();
var firstDay = 1;
var LastDate = new Date(year, month + 1, 0);
var LastDay = LastDate.getDate();
var today = new Date();
var TodayMonth = today.getMonth();
var date = true;
if (month <= TodayMonth) {
jQuery.noConflict();
swal("Error!", "Please select upcoming month!", "warning");
$('#lease_stattdate').val('');
$('#renewal_notice').val('');
$('#end_date').val('');
$('#termination_notice').val('');
window.location.reload();
}
if (month > TodayMonth && dateSelected > firstDay) {
jQuery.noConflict();
swal("Error!", "Please select first day of the month!", "warning");
$('#lease_stattdate').val('');
$('#renewal_notice').val('');
$('#end_date').val('');
$('#termination_notice').val('');
window.location.reload();
}
if (month > TodayMonth && dateSelected == firstDay) {
var tempday = 15;
var nextdate = new Date(year, month + 23, tempday);
var NXmonth = nextdate.getMonth();
var NXyear = nextdate.getFullYear();
var terminateday = new Date(NXyear, NXmonth + 1, 0).getDate();
var exitdate = new Date(NXyear, NXmonth, terminateday).toLocaleDateString('en-ZA');
var renewalNotice = new Date(NXyear, NXmonth-2, 1).toLocaleDateString('en-ZA');
var terminationNotice = new Date(NXyear, NXmonth, 1).toLocaleDateString('en-ZA');
$('#renewal_notice').val(renewalNotice);
$('#end_date').val(exitdate);
$('#termination_notice').val(terminationNotice);
}
}
There is a variable which contains event time. I want to redirect user if event time + 04:38 is more than current time.
Below is the code i have tried:
var deadline = getDayInstance("Monday","08:00:59")
function getDayInstance(day,time) {
const days = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,"Friday":5,"Saturday":6};
if(days[day]!==undefined)
var dayINeed = days[day];
else
var dayINeed = 2; // for Tuesday
const today = moment().tz("America/Los_Angeles").isoWeekday();
var dt;
// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
dt = moment().tz("America/Los_Angeles").isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
else {
dt = moment().tz("America/Los_Angeles").add(1, 'weeks').isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
console.log("Event Time: "+dt);
var maxLimit = Date.parse(moment(time,'HH:mm:ss').tz("America/Los_Angeles").add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
var now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit < now)
{
dt = moment().tz("America/Los_Angeles").add(1, 'weeks').isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
window.setVideo = false;
}
console.log(moment(time,'HH:mm:ss').tz("America/Los_Angeles").add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
console.log(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
return dt;
}
var maxLimit = Date.parse(moment(deadline,'YYYY-MM-DDTHH:mm:ss').add({"hours":4,"minutes":43,"seconds":33}).format("YYYY-MM-DDTHH:mm:ss"));
var now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit>=now){
var redirectTo = $("#lp-pom-button-673").attr('href');
if(redirectTo.length > 3){
window.location.href = redirectTo;
}
else{
window.location.href = "https://******/";
}
}
I need to maintain timezone in calculation.
I got the answer of this after refining the process theoretically and then try to implement it in JS. Below is the new code that can be used for this.
var deadline = getDayInstance("Tuesday", "02:32:00");
var maxLimit,now;
function getDayInstance(day,time) {
const days = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,
"Friday":5,"Saturday":6};
if(days[day]!==undefined)
var dayINeed = days[day];
else
var dayINeed = 2; // for Tuesday
const today = moment().tz("America/Los_Angeles").isoWeekday();
var dt;
// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
dt = moment().tz("America/Los_Angeles").isoWeekday(dayINeed)
.format('YYYY-MM-DD')+"T"+time;
}
else {
dt = moment().tz("America/Los_Angeles").add(1, 'weeks')
.isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
var d = new Date(dt);
d.setHours(d.getHours() + 4);
d.setMinutes(d.getMinutes() + 43);
d.setSeconds(d.getSeconds() + 32);
max = Date.parse(d);
now = Date.parse(moment().tz("America/Los_Angeles").format('YYYY-MM-DDTHH:mm:ss'));
if(maxLimit < now)
{
dt = moment().tz("America/Los_Angeles").add(1, 'weeks')
.isoWeekday(dayINeed).format('YYYY-MM-DD')+"T"+time;
}
return dt;
}
if(maxLimit>=now){
var redirectTo = $("#lp-pom-button-673").attr('href');
if(redirectTo.length > 3){
window.location.href = redirectTo;
}
else{
window.location.href = "https://******/";
}
}
I am looking for help why this code is not working for all headings of column. This code works for fist time for any column but does not fire click event second time. why $(ColTh).click(function () is not being attached to all th in header ?
$("table.ui-datepicker-calendar > thead").find("th[scope='col']").each(function() {
var ColTh = $(this);
$(ColTh).click(function() {
var colIndex = $(ColTh).index();
var dateList = [];
$(ColTh).closest('table').find('tr').each(function() {
var dateTd = $(this).find('td:eq(' + colIndex + ')');
var month = $(dateTd).attr('data-month'); //.trigger('click');
var year = $(dateTd).attr('data-year');
var day = $(dateTd).children(0).text();
if (typeof month != 'undefined') {
var monthINT = parseInt(month.trim());
monthINT = monthINT + 1;
var newDate = monthINT + '-' + day.trim() + '-' + year.trim();
dateList.push(newDate);
}
});
$('#itemSchedulerCal').multiDatesPicker('addDates', dateList);
dateList = [];
});
});
I'm not sure about the rest of your code, but about your binding, you can do like this:
$("table.ui-datepicker-calendar").on("click", "th[scope='col']", function() {
var ColTh = $(this);
var colIndex = ColTh.index();
var dateList = [];
ColTh.closest('table').find('tr').each(function() {
var dateTd = $(this).find('td:eq(' + colIndex + ')');
var month = dateTd.attr('data-month'); //.trigger('click');
var year = dateTd.attr('data-year');
var day = dateTd.children(0).text();
if (typeof month != 'undefined') {
var monthINT = parseInt(month.trim());
monthINT = monthINT + 1;
var newDate = monthINT + '-' + day.trim() + '-' + year.trim();
dateList.push(newDate);
}
});
$('#itemSchedulerCal').multiDatesPicker('addDates', dateList);
dateList = [];
});
You are using to ColTh as $(ColTh), but ColTh is already a jQuery object since you are setting it with $(this). The same applies to dateTd.
i did following steps to achieve above
Step 1:
jQuery(document).ready(function () {
$.datepicker._updateDatepicker_original = $.datepicker._updateDatepicker;
$.datepicker._updateDatepicker = function (inst) {
$.datepicker._updateDatepicker_original(inst);
var afterShow = this._get(inst, 'afterShow');
if (afterShow) {
// trigger custom callback
afterShow.apply((inst.input ? inst.input[0] : null));
}
}
});
step 2:
$('#itemSchedulerCal').multiDatesPicker({
showWeek: true,
minDate: 0,
dateFormat: "m-d-yy",
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
defaultDate: defdate,
afterShow: function (inst)
{
BindClicks();
}
});
Step 3:
function BindClicks() {
// paste all above code
}
I have the following string:
bar
So I can pull the title as a string, but I want to replace the {m{month}m} with {m{August}m} and it needs to be flexible enough so I can also then again replace it with {m{September}m}
I currently have this:
var thetitle = $(this).attr('title');
var newtitle = thetitle.replace("{m{month}m}",months[currentmonth]);
And this replaces fine the 1st time, but then it strips out the entire value and replaces "{m{month}m}" with say "August", so when I need to replace it again I can't target it.
Any help would be appreciated.
G
Try to use
var changeMonth = (function(){
var previous = 'month' ;
return function(title,month){
var temp = previous;
previous = month
return title.replace('m{' + temp + '}m', month);
}
})();
This works for me. Try
var changeMonth = (function(){
var previous = 'month' ;
return function(title,month){
var temp = previous;
previous = month;
return title.replace('m{' + temp + '}m','m{'+ month + '}m');
}
})();
var input = $('input');
var a = $('a');
$('button').on('click', function(){
var month = input.val();
var newMonth = changeMonth($(a).attr('title'), month);
$(a).attr('title', newMonth);
console.log($(a).attr('title'));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Your month here<input type='text'>
<button>Change</button>
var title = "blahblah?foo=1&month={m{August}m}";
var previousmonth = "August";
var previousmonthstr = "{m{" + previousmonth + "}m}";
var currentmonth = "September";
var currentmonthstr = "{m{" + currentmonth + "}m}";
var newtitle = title.replace(previousmonthstr, currentmonthstr);
// Your title is stored in newtitle
View live demo here
Well, here is the json file http://herbalista.hol.es/group.json i am working with JSON.parse(); on Google apps script. I temporarily solve with this code by Choosing the post which have more than 15 likes, but i want to choose the one with more likes independently if have or not more than 15 likes.
function repost() {
var UsrAccess_token = "xxxxxxxxx"
var graph = "https://graph.facebook.com/xxxxxx/feed/?access_token="+UsrAccess_token+"";
var jsondata = UrlFetchApp.fetch(graph,{method:"get"}).getContentText();
var object = JSON.parse(jsondata);
var item = object.data;
var currentTime = new Date();
var year = currentTime.getUTCFullYear();
var month = (currentTime.getUTCMonth()) + 1;
var day = (currentTime.getUTCDate()) - 1;
if (day <= 9) {var day = "0"+day+"";}
if (month <= 9) {var month = "0"+month+"";}
var utime = ""+year+"-"+month+"-"+day+"T";
try {
var i = null;
for (i = 0; item.length > i; i += 1) {
var pubDate = item[i].created_time;
if (pubDate.match(utime)) { var likesdata = item[i].likes.data; var len = likesdata.length;
if (len > 15) {var popular = item[i].link;}}
}} catch(err) {
var err = "ERROR";
}
}
For this you can Choose a default value for a variable like var maxLikes = 0; and verify against len variable.
The code would be something like this:
function repost() {
var UsrAccess_token = "xxxxxxxxx"
var graph = "https://graph.facebook.com/xxxxxx/feed/?access_token="+UsrAccess_token+"";
var jsondata = UrlFetchApp.fetch(graph,{method:"get"}).getContentText();
var object = JSON.parse(jsondata);
var item = object.data;
var currentTime = new Date();
var year = currentTime.getUTCFullYear();
var month = (currentTime.getUTCMonth()) + 1;
var day = (currentTime.getUTCDate()) - 1;
if (day <= 9) {var day = "0"+day+"";}
if (month <= 9) {var month = "0"+month+"";}
var utime = ""+year+"-"+month+"-"+day+"T";
try {
var i = null;
var maxLikes = 0;
for (i = 0; item.length > i; i += 1) {
var pubDate = item[i].created_time;
if (pubDate.match(utime)) {
var likesdata = item[i].likes.data;
var len = likesdata.length;
if (len > maxLikes) {
maxLikes = len;
var popular = item[i].link;
}
}
}
} catch(err) {
var err = "ERROR";
}
}
Hope that helps!