CSS 3 selectors seem to be my solution: $('inp...@id^="foo"]...@id$="bar"]').change(function(){ alert(this.id); }
This will be attached to all input fields that have an ID starting with 'foo' and ending with 'bar' (e.g. 'foo123bar', 'foobar', 'foo456bar'). Then within the function I can pass through the actual ID with 'this.id'. Cheers. On Dec 14, 1:44 pm, "Dan Switzer" <dswit...@pengoworks.com> wrote: > The easiest way to do this would be to give each div a specific class: > > <div id="mydiv1" class="foo" /> > <div id="mydiv2" class="foo" /> > <div id="mydiv3" class="foo" /> > <div id="mydiv4" class="foo" /> > > Now you could just do: > > $("div.foo").change(); > > I like this method, since usually this divs have related visuals, so > you may already have a constant class defined for the elements. > > An alternative would be to do something like: > > $("div[id^=mydiv]").change(); > > This would find all divs with an ID that starts "mydiv". The > performance on the class method may be more efficient though. > > -Dan > > On Sat, Dec 13, 2008 at 8:30 PM, Daniel L <dan...@cleardocs.com> wrote: > > > Hi, I have a situation where the same javascript is repeated about 50 > > times - just with a differnt ID. Example: > > > $('#mydiv1').change(function() { > > doStuff('#mydiv1'); > > }); > > $('#mydiv2').change(function() { > > doStuff('#mydiv2'); > > }); > > ... > > $('#mydiv50').change(function() { > > doStuff('#mydiv50'); > > }); > > > Is there a way to combine all these calls using wildcards?? I imagine > > it would be something like: > > > $('#mydiv<* as foo>').change(function() { > > doStuff('#mydiv<foo>'); > > }); > > > Any help would be greatly appreciated.