If you don't have control over the php then ... $('a[id^=showtxt]').click(function() { var divID = this.id.replace(/^showtxt(\[)(\d+)(\])$/, '#textbox \\$1$2\\$3'); $(divID).slideToggle(400); return false; });
If you DO have control over the php then I would recommend changing the your ids to use something like showtxt_1 and textbox_1, rather than putting square brackets in the id - it makes life far simpler (and the document possibly more compliant). You could further simplify your script by using classes in your HTML. On Oct 11, 11:23 pm, "Yasmary Mora" <[EMAIL PROTECTED]> wrote: > Hello! > > Got a question that I havn't been able to answer. This is my last resort. :) > > So, I have a list of dynamically created divs, with a link next to > them to show or hide text inside of it. I know how to do it when its > only a single box, but I dont know how to do it with a dynamic list. > > The whole script gets created with php, so I don't know how many divs > there'll be. Normally I do the whole thing with javascript but I > really like jQuery, and I wanted to try to stick to using it. > > So the result of the php script is something like this: > > <a href="#" id=showtxt[1]">show text</a><div id="textbox[1]">Text here</div> > <a href="#" id=showtxt[2]">show text</a><div id="textbox[2]">Text here</div> > <a href="#" id=showtxt[3]">show text</a><div id="textbox[3]">Text here</div> > <a href="#" id=showtxt[4]">show text</a><div id="textbox[4]">Text here</div> > > The jQuery code I have works for only one box... > > $('#showtxt').click(function() { > $('#textbox').slideToggle(400); > return false; > > }); > > I've tried for the past 2 hours to figure it out, and I've had no > luck. I think I might be using the wrong wording to describe my > problems, but either way I have no idea exactly what I'm looking for. > Any help is appreciated. :o) > > Thanks in advance! > > -- > -Yasmary