xander85 Posted September 20, 2011 Share Posted September 20, 2011 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 More sharing options...
AyKay47 Posted September 20, 2011 Share Posted September 20, 2011 probably with a for loop.. can you give a quick explanation further explaining the logic here? Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1270989 Share on other sites More sharing options...
xander85 Posted September 20, 2011 Author Share Posted September 20, 2011 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. Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1270993 Share on other sites More sharing options...
xander85 Posted September 20, 2011 Author Share Posted September 20, 2011 The logic is the following: 1,2 3,4 5,6 etc... Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1270994 Share on other sites More sharing options...
xander85 Posted September 20, 2011 Author Share Posted September 20, 2011 I tried the following, but it's not working. I think I'm close though: for(i=1; i<=30; i+=2) { $("#time1000" + i ", #time1000" + i+1 ").timePicker(); } Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1271001 Share on other sites More sharing options...
AyKay47 Posted September 20, 2011 Share Posted September 20, 2011 perhaps for(i=1; i<=30; i++) { x=2; $("#time1000" + i , #time1000" + x).timePicker(); x++; } Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1271002 Share on other sites More sharing options...
xander85 Posted September 20, 2011 Author Share Posted September 20, 2011 That doesn't work. Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1271007 Share on other sites More sharing options...
nogray Posted September 20, 2011 Share Posted September 20, 2011 It doesn't work because the syntax is incorrect. It should be $("#time1000" + i +", #time1000" + x).timePicker(); Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1271120 Share on other sites More sharing options...
goodacre.liam Posted September 24, 2011 Share Posted September 24, 2011 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. Link to comment https://forums.phpfreaks.com/topic/247509-simple-loop-question/#findComment-1272214 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.