Jump to content

Simple Loop Question


xander85

Recommended Posts

Hi,

 

I'm pretty new to Javascript (have a bit of PHP experience) and have a quick loop question. I have the following code:

 jQuery(function() {
 $("#time10001, #time10002").timePicker();
 $("#time10003, #time10004").timePicker();
 $("#time10005, #time10006").timePicker();
 $("#time10007, #time10008").timePicker();
 $("#time10009, #time10010").timePicker();
 $("#time10011, #time10012").timePicker();
 $("#time10013, #time10014").timePicker();
 $("#time10015, #time10016").timePicker();
});

 

Is it possible to do this with a loop? I'd like to be able to change the amount of "timepicker's" dynamically?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/247509-simple-loop-question/
Share on other sites

The "datepicker()" function is used to make two drop-down menus into a menus that allow you to select times. I just need to repeat each line for the amount of "date picker's" I have setup. I just need to repeat the following line:

 

$("#timeX, #timeX+1").timePicker()

 

X amount of times.

 

Just not sure how the for loop syntax works exactly for javascript.

How about this?

jQuery(function($) {
    $('[id^="time"]').timePicker();
});

 

This selects all those which have an id starting with "time".

 

Alternatively just give them all the same class:

jQuery(function($) {
    $('.time').timePicker();
});

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.