Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.