How to bind dropdown values to javascript array? - javascript

I am working in MVC and I have to bind dropdown values to an array in javascript. How do I do that?
dropdown list-
Html.DropDownListFor(m => m.MyActivityRequest.ToMonth, Model.MonthNames, new { #id = "monthToSelect" })
Javascript function:
$(document).ready(function(){
$("#yearToSelect").change(function(){
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date();
var monthVar = date.getMonth();
var yearVar = date.getFullYear();
if ($(this).val() < yearVar ){
$("#monthToSelect").find('option').remove();
for( i = 0; i < 12; i++ ){
$("#monthToSelect").append($("<option>",{
value: months[i],
text: months[i]
}));
};
}
you can see that the the array- 'months' is hard coded as of now. I want to dynamically bind the dropdown values to this array.

Js Fiddle Demo.
Inside your code place the below code appropriately.
Better Approach:
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var option = '';
for (i=0;i<months.length;i++){
option += '<option value="'+ months [i] + '">' + months [i] + '</option>';
}
$('#monthToSelect').append(option);
Refered from here.

I think you have to pass the month list from controller. So in javascript you can make an ajax call and return the same list of months. You can use this list instead of array- 'months' in your javascript function.

Related

Date format in JavaScript. For example: June 10, 2021 [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
How do I can set the date format to "June 10, 2021"?
I have a small piece of code like this:
var homnay = new Date();
var datehomnay = (homnay.getMonth()+1) + ' ' + homnay.getDate() + ', ' + homnay.getFullYear();
document.getElementById("curDay").innerHTML = datehomnay;
Use this procedure
const MONTHS = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']
const date = new Date()
const dateString = `${MONTHS[date.getMonth()]} ${date.getDate()}, ${date.getFullYear()}`
console.log(dateString)

How to get check for current month in Daterangepicker?

I want to do something like this, I tried multiple ways but I am unable to find it. I am getting start and end date but not the current month. I want current month so it works with all locale
if(CURRENT MONTH){
// Do This
}
use the Date object for get month as follows,
var date = new Date(); // assume date is 10/06/2020
var month = date.getMonth(); // it return month as 10
if you want to get month name, firstly you should declare the array including with months and then get the month name using this array as follows,
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var date = new Date(); // assume date is 10/06/2020
var nameOfMonth = months[date.getMonth()-1] // it return month name as October
then you can use month or nameOfMonth as follows,
if (month == 10){
// Do This
}
if (nameOfMonth == 'October'){
// Do This
}

sum of variable in for(variable in object) loop is resulting in concatenation JavaScript [duplicate]

This question already has answers here:
for( in ) loop index is string instead of integer [duplicate]
(5 answers)
Closed 2 years ago.
i'm new to JavaScript. Im trying to make a loop that prints every month and also numbers them, but i end up getting a concatenation of the numbers. here's my code
var months = ['january', 'february', 'march','april',
'may', 'june', 'july', 'august',
'september', 'october', 'november, 'december'];
for(i in months){
document.write((i+1)+'.- '+months[i]);
document.write('<br >')
}
and my output is like this:
01.- janury
11.- february
21.- march
.... etc
This code will give you the desired result, changing it to a standard for loop:
var months = ['january', 'february', 'march','april', 'may', 'june', 'july', 'august' 'september', 'october', 'november', 'december'];
for (i=0; i < months.length; i++) {
document.write(i+1 + ".- " + months[i] + "<br>")
};

Is it possible to pop up a html element?

My requirement is like I have a textarea which is disabled. On click of a button the user should be prompted for a datetime and it should be appended to the textarea value with a newline so that I can read it later one by one. Because I need to store these datetime values in an xml file which would be used to remember these values.
I am looking for a way to get a datetime input on click of a button. I thought to popup a datetime element to get the input using jquery. But I am not sure will it work or not. Is there any way to do it ?
<textarea id="schedule" name="schedule" placeholder="Mailing Schedule's" class="inputarea" disabled="disabled"></textarea>
$('#addschedule').click(function(){
var schedule=prompt("Enter Schedule");
var val = $("textarea#schedule").val();
if(val!="")
$("textarea#schedule").val(val+"\n"+schedule);
else
$("textarea#schedule").val(schedule);
});
I tried it for text input. But I dont know how to do it for datetime. Sorry If its not that good question. But some help would be appreciated.
$(function(){
$('button').click(function(){
var d = new Date();
var t = d.toLocaleTimeString();
var mL = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var mS = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
var week = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"];
var day = week[d.getDay()];
var date = d.getDate();
var month = mL[d.getMonth()];
var year = d.getFullYear();
var total = day+' '+date+' '+month+' '+year+' '+t+'\n';
var schedule=prompt("Enter Schedule", 'here');
var val = $("textarea#schedule").val();
if(val!=="")
$("textarea#schedule").val(val+'\n'+total+' '+schedule);
else
$("textarea#schedule").val(total+' '+schedule);
});
});

Javascript convert date format from "yyyy-mm-dd" to "December 11th, 2013"?

How I make a sintax to convert date format from default format (yyyy-mm-dd) to english format like December 11th, 2013 using Javascript function?
Thank you
You could use moment.js
Moment.js 2.7.0
Moment was designed to work both in the browser and in Node.JS.
Parse, validate, manipulate, and display dates in javascript.
and it is also available on cdnjs.com.
var str_date = "1983-24-12",
arr_date = str_date.split('-'),
months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var new_date=months[arr_date[2]] + ' ' + arr_date[1] + ', ' + arr_date[0]);
http://jsfiddle.net/TN7NE/
Edit
You can add an ordinal using the following:
function addOrdinal(n) {
var ord = [,'st','nd','rd'];
var a = n%100;
return n + (ord[a>20? a%10 :a] || 'th');
}
e.g.
addOrdinal(1); // 1st
You can use a function like this:
function formatDate(date) {
months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
dateSplit = date.split('-');
year = dateSplit[0];
month = months[parseInt(dateSplit[1]) - 1];
day = parseInt(dateSplit[2]);
switch(day) {
case 1:
case 21:
case 31:
day += 'st';
break;
case 2:
case 22:
day += 'nd';
break;
case 3:
case 23:
day += 'rd';
break;
default:
day += 'th';
}
return month + ' ' + day + ', ' + year;
}

Categories

Resources