Chama
Chama

Reputation: 199

jQuery: Use the same function for multiple ids

i'm using this

jQuery('#mydiv').datetimepicker({ ... });

How can i use this function for more than just one ID?

Thought something like this should do the job but it doesn't :(

jQuery('#mydiv','#anotherdiv).datetimepicker({ ... });
jQuery('#mydiv|#anotherdiv).datetimepicker({ ... });

Any ideas? Thanks!

Upvotes: 0

Views: 103

Answers (3)

Barmar
Barmar

Reputation: 782693

Use a single argument with the IDs separated with commas, not separate arguments.

jQuery('#mydiv,#anotherdiv').datetimepicker({ ... });

Upvotes: 2

RToyo
RToyo

Reputation: 2877

A common class would be the best solution, as @simon suggested.

If you absolutely need to use IDs (eg you can only modify the JS, and not the DOM), you can loop through an array of IDs:

$.each(['mydiv', 'anotherdiv'], function (index, value)
{
    $.('#' + value).datepicker({ ... });
}

Upvotes: 0

Simon
Simon

Reputation: 784

Give the respective Divs a class name, then reference them like this:

jQuery('.className').datetimepicker({ ... });

Upvotes: 0

Related Questions