How to increment textbox (month)name on button click in javascript - javascript

I want to increase and decrease a text box name on click button
here an image i post to give clarification of what i have to do exactly
i tried but text value increaseing but i wana show month name in that text box
Any help is appreciated.

You could try with Array .create the array for month .And apply the increment and decrements value with array arguments position.Ternary operator used for restrict the count below 0 and above 11
var month = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var c =0;
$('#mins').click(function(){
c = c-- > 0 ? c-- : 0;
$('#mnt').val(month[c])
})
$('#plus').click(function(){
c = c++ < 11 ? c++ : 11;
$('#mnt').val(month[c])
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="mnt" value="January">
<button id="mins">-</button>
<button id="plus">+</button>

Create an Array of month names, a starting variable to represent the current location in the Array, and a variable to hold the input field for the Month:
var months = ["January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"];
var currentMonth = 0;
var monthInput = $("#month-input");
Now add click events to the + and - buttons. You'll need to add an id attribute to each one.
$("#subtract-month-button").click(subtractMonth);
$("#add-month-button").click(addMonth);
Finally, your methods to handle the click events.
function subtractMonth () {
if (currentMonth > 0) {
currentMonth--;
}
monthInput.val(months[currentMonth]);
}
function addMonth () {
if (currentMonth < months.length - 1) {
currentMonth++;
}
monthInput.val(months[currentMonth]);
}

Related

How to get previous month name on given some month (in words not numerical value)? [duplicate]

This question already has answers here:
How to get previous month (DECEMBER) name
(6 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I want a function that gives the full name of prev month for any given month[not just current month]
Ex: prevMonth(August) = July; prevMonth(January)= December.
I'm new to js and can't figure out how to use this array to get the result:
monthsarray: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Are expecting a simple solution like this?
var monthsarray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
function prevMonth(curMonth) {
var curMonthIndex = monthsarray.indexOf(curMonth);
var prevMonthIndex;
if (curMonthIndex == 0) prevMonthIndex = monthsarray.length - 1;
else if (curMonthIndex == monthsarray.length - 1) prevMonthIndex = 0;
else prevMonthIndex = curMonthIndex - 1;
return monthsarray[prevMonthIndex];
}
console.log(prevMonth("August"));
console.log(prevMonth("December"));
let monthsarray1= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] ;
let m="May";
console.log(pvsMonth(m))
function pvsMonth(m){
let ln=monthsarray1.length
let idx=monthsarray1.indexOf(m)
if(idx==0){
return monthsarray1[ln-1]
}else{
return monthsarray1[idx-1]
}
}
function prevMonth(month) {
const index = monthsarray.indexOf(month);
if (index > 0) {
return monthsarray[index - 1];
}
if (index === 0) {
return monthsarray[monthsarray.length - 1];
}
throw new Error('Invalid Month given');
}

problem while creating dynamic labels for chart js

I am creating dynamic labels for the chart js by supplying range of the month . It is working good if i selected start month and end month in ascending order but it is not working in the case where i selected start month = december and end month = march.
Here is my code ,
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
return monthArr;
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March'];
if (monthNames.indexOf(monthn[1]) < monthNames.indexOf(monthn[0])) {
monthNames.unshift(monthNames.splice(monthNames.indexOf(monthn[0]), 1)[0]);
for (var i = monthNames.indexOf(monthn[0]); i <= diff; i++) {
monthArr.push(monthNames[i]);
}
} else {
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
}
return monthArr;
Try to validate the index between the two months
Here is an example with your code and a very small changes.
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
// make sure that StartMonth is smaller then Endmonth
var startMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[0]) : monthNames.indexOf(monthn[1]);
// make sure that EndMonth is bigger then StartMonth
var endMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[1]) : monthNames.indexOf(monthn[0]);
for (var i = startMonth; i <= endMonth; i++) {
monthArr.push(monthNames[i]);
}
// Add the missing months
if (startMonth -1 >0)
{
for (var i = 0; i <= startMonth -1; i++) {
monthArr.push(monthNames[i]);
}
}
console.log(monthArr)

Results from loop don't work correctly

I want to loop through an array.
Any index which has a length larger than 3 I want to abbreviate.
I want to place the new abbreviated months into a new array.
I want to test that it works by writing the results to the console.
I can get the code to run, however the results don't come out the way I'd hope. From my understanding the loop runs true, running the if statement, which runs true, running the code block. After that the loop should iterate and continue as long as i < months.length, but it doesn't.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev = months[i].slice(0, 3);
}
}
console.log(monthsAbbrev);
You can do it simply with Array.prototype.map()
var months = ["January", "Febuary"]; //sample data for better understanding
var monthsAbbrev = months.map(v => v.substr(0,3));
console.log(monthsAbbrev); //["Jan", "Feb"]
By the way you are not pushing anything into your target array in your code, that is the problem.
Beside the pushing, you can omit the check for length > 3, because slice is already doing it and that prevent to miss some month, like 'May'.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
monthsAbbrev.push(months[i].slice(0, 3));
}
console.log(monthsAbbrev);
You need to add the months to your monthsAbbrev array. One way you can do this is by using the .push() function:
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev.push(months[i].slice(0, 3));
}
}
console.log(monthsAbbrev);
You just had to push to the array formed. Rest you already had it.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev.push(months[i].slice(0, 3));
}
}
console.log(monthsAbbrev);

Looping through an array to div?

I'm trying to add a p element containing a single month in this string to a div with the id of "write". How do I get this loop to run through the list of strings and add them to the div?
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November", "December"];
var element = document.getElementById("write").innerHTML = months;
for (var i = 0; i <= months; i++) {
var para = document.createElement("p");
var text = document.createTextNode(i);
}
I don't understand why it isn't working. Thanks!
It looks like you're totally misunderstanding how to use arrays, you need to go back to your tutorials and study this.
You need to use months.length as the limit of the array, not just months. You should use <, not <= in the test, because otherwise you'll go past the end of the array. And in the text node you should put months[i], not just i. Then you need to make the text node a child of the paragraph. You can't set the innerHTML of an element to an array; you should be adding the paragraphs as children of the element.
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var element = document.getElementById("write");
for (var i = 0; i < months.length; i++) {
var para = document.createElement("p");
var text = document.createTextNode(months[i]);
para.appendChild(text);
element.appendChild(para);
}
<div id="write"></div>

Is there a JQuery API for assigning the month name based on the number.?

Am working on SharePoint List, fetching the date from the list, which is generally in this format : 2013-03-25.
I have split the date and assigned to an variable.
Code.
var splitDate = resultRegionArr[x].NewsDate.split("-");
Now, i want to know if there is any Jquery API which returns me the string "March" on input of value "03" or splitDate[1].
Thanks in advance.
var months = [ "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" ];
var selectedMonthName = months[splitDate[1] - 1];
The - 1 is optional. Depending on your input/output of splitDate[1].
try
www.datejs.com/, for example:
alert(Date.parse('2013-03-25').toString('dd MMMM yyyy')) // 25 March 2013
this will give you what you want also bonus is other features!!

Categories

Resources