0

The jQuery documentation for the Datepicker says that I can set the year range after initialization by doing the following:

$( ".selector" ).datepicker( "option", "yearRange", "2002:2012" );

I'd like to know how I'd be able to, instead of manually inputing the years, use a previously set variable to set the year range.

alcova
  • 29
  • 5
  • Unless i'm misunderstood all you need to do is set the variable as you would any other and then put it in the place of the value? But that is so obvious I am not sure it's what your asking – akaBase Nov 17 '20 at 12:09
  • Meaning set the vaiable `var yearRange = "2002:2012"` and then use it `$( ".selector" ).datepicker( "option", "yearRange", yearRange );` – akaBase Nov 17 '20 at 12:11
  • I have two different variables that constitute the yearRange, should I be able to set them like `var yearRange = variable1:variable2` or something like that? – alcova Nov 17 '20 at 12:17
  • You can concatonate them with the colon `var yearRange = variable1 + ":" + variable2` Check out [this](https://stackoverflow.com/questions/31845895/whats-the-best-way-to-do-string-building-concatenation-in-javascript) for more info on string concatonation – akaBase Nov 17 '20 at 12:24

1 Answers1

0

If your variable is derived from more than one values/variables you can use one of the following methods to concatenate the values:

template literals (BEST)

var yr1 = "2002";
var yr2 = "2012";
var yrRange = `${yr1}:${yr2}`;

The + operator (as suggested in the comments)

var yr1 = "2002";
var yr2 = "2012";
var yrRange = yr1 + ":" + yr2;

Other approaches include String#concat() and Array#join() methods

var yrRange = yr1.concat(":",yr2);

var yrRange = [yr1, yr2].join(":");

DEMOS

    //template literals (BEST)
    var yr1 = "2002";
    var yr2 = "2012";
    var yrRange = `${yr1}:${yr2}`;
    console.log( yrRange );

//string concatenation (as suggested in the comments)
    var yr1 = "2002";
    var yr2 = "2012";
    var yrRange = yr1 + ":" + yr2;
    console.log( yrRange );
    console.log( yr1.concat(":",yr2) );
    console.log( [yr1,yr2].join(':') );
PeterKA
  • 24,158
  • 5
  • 26
  • 48