1

I have an element that is repetitive. I tried to use it more than once but couldn't. I end up doing multiple times.

$(function(){
   $('#schstart1').datepicker({
      format: 'mm-dd-yyyy',
      startDate: '01-01-1900'
    });  
    $('#schend1').datepicker({
      format: 'mm-dd-yyyy',
      startDate: '01-01-1900'
    });  
    $('#schstart2').datepicker({
      format: 'mm-dd-yyyy',
      startDate: '01-01-1900'
    });  
....
...
..

Is there anyway i can define #schstart only and reuse many times?

OneNation
  • 427
  • 1
  • 8
  • 22

3 Answers3

0

This should work the same (just modify selector):

$('#schstart1, #schend1, #schstart2').datepicker({
  format: 'mm-dd-yyyy',
  startDate: '01-01-1900'
});
Anarion
  • 1,048
  • 6
  • 16
  • Do i need to define the selectors? Is there any way I can use #schstart only for more than 1 input field? – OneNation Oct 01 '14 at 01:35
0

Use class instead. Add class needDatepicker to each element, then use

$(".needDatepicker").datepicker({...})

wander
  • 937
  • 1
  • 7
  • 15
0

Define it as a variable starting with $, call it whatever you like. It now represents a jQuery object and is saved as a variable so you can use it over and over without referencing it again to save memory.

var $schstart1 = $('#schstart1');

$schstart1.datepicker({
    format: 'mm-dd-yyyy',
    startDate: '01-01-1900'
});
$schstart1.datepicker({
    format: 'mm-dd-yyyy',
    startDate: '01-01-1900'
});

Check out this good answer HERE if you'd like to learn more about jQuery object variables and how they work.

Community
  • 1
  • 1
Fizzix
  • 23,679
  • 38
  • 110
  • 176