tags:

views:

35

answers:

2

Hi all

I have a form where I've got multiple text inputs for various timefields, each with a unique ID set for some other code that I'm using. I'm also trying to use a time entry script that has a simple single line bit of code to implement, but as I have 28 different fields all with different IDs, this is going to get repetitve fast. Is there a way to reference in the jquery code to reference the same function across multiple IDs without duplicating the typing?

example:

html

<input id="M_start_time1" />
<input id="M_end_time1" />
<input id="M_start_time2" />
<input id="M_end_time2" />

jquery

$('#M_start_time1').timeEntry({
    ampmPrefix: ' ',
});
$('#M_end_time1').timeEntry({
    ampmPrefix: ' ',
});
$('#M_start_time2').timeEntry({
    ampmPrefix: ' ',
});
$('#M_end_time2').timeEntry({
    ampmPrefix: ' ',
});

Any suggestions are greatly appreciated! :)

+3  A: 

Give your input fields a common class:

<input id="M_start_time1" class="something" />
<input id="M_end_time1" class="something" />

and use a single class selector to do all the work:

$('.something').timeEntry({
    ampmPrefix: ' ',
});
casablanca
It would be better to limit the selector to something like `$("input:text.something")`.
rahul
Why is that even needed? The class should be added only to those elements that require it, in which case `input:text` is redundant.
casablanca
@casablance - since it's much more efficient. `.something` uses jQuery to search through every single element. `input.something` only needs to use jQuery on all `input` elements.
Peter Ajtai
`input:text` only iterates through input element which are textboxes only.
rahul
the class works in some instances, but my problem is that some of my elements already have classes assigned due to other events being called that rely on the classes (it's getting complicated).
Aninemity
@Peter Ajtai: I guess that makes sense, but at least Firefox already has a native `getElementsByClassName` and others should soon follow suit.
casablanca
@Aninemity: You could always have multiple classes on the same element.
casablanca
@casablanca: do you have to do anything special to make it recognize the second class? anytime i've tried it in the past it blows up on me and I haven't had the time to sit down and research what i'm doing wrong yet so i've been avoiding that.
Aninemity
@Aninemity - Nothing special. But if you already have a class name/s present use `.addClass("newClass")` instead of `.attr("class","newClass")` so you don't overwrite stuff. You only have to include one class name when selecting the items later.
Peter Ajtai
cool. thanks for that. will give it a go. thanks muchly
Aninemity
+2  A: 

You could use the jQuery starts with attribute selector if all your ids of interest start with something, and none of the ids of inputs you're not interested in start with that:

$('input[id^=M_]').timeEntry({
    ampmPrefix: ' '
});

Try out the starts with attribute selector with this jsFiddle

Peter Ajtai