[jQuery] Re: improve my working code
How he said is the better way, i think $(".classLink").click( function(){ $(".classDetail").slideToggle("slow"); return false; }); On 5/23/07, Richard D. Worth <[EMAIL PROTECTED]> wrote: On 5/23/07, tlob <[EMAIL PROTECTED]> wrote: > > [code] > $(document).ready(function(){ > $("#linkDetail1").click(function(){ > $("#detail1").slideToggle("slow"); >return false; > }); > > $("#linkDetail2").click(function(){ > $("#detail2").slideToggle("slow"); > return false; > }); > > $("#linkDetail3").click(function(){ > $("#detail3").slideToggle("slow"); > return false; > }); > } > [/code] $(["#linkDetail1", "#linkDetail2", "#linkDetail3"]).click(function(){ $(this).slideToggle("slow"); return false; }); > what if I have to extend the IDs #linkDetail4, #linkDetail5, . > > You'll likely want to find a different selector in that case. Maybe give them each the same class and use $(".className").click(... - Richard D. Worth -- []´s Jean www.suissa.info Ethereal Agency www.etherealagency.com
[jQuery] Re: improve my working code
tlob wrote: Hy there. I'm pretty new to jQuery and JS in general. I managed to build something. I learned a lot! THX jQuery guys. When you look into my code, could it be shorter, smarter, sharper? especially this part: [code] $(document).ready(function(){ $("#linkDetail1").click(function(){ $("#detail1").slideToggle("slow"); return false; }); $("#linkDetail2").click(function(){ $("#detail2").slideToggle("slow"); return false; }); $("#linkDetail3").click(function(){ $("#detail3").slideToggle("slow"); return false; }); } This might do what you want, replacing all those: $("div.details").each(function() { var elt = $(this); $("#linkD" + this.id.substring(1)).click(function() { elt.slideToggle("slow"); return false; }); }); It would be simpler still except for the need to work around a name mismatch in detail2 versus linkDetail2. If you changed your markup slightly, you could use the simpler $("#link-" + this.id).click(function() { Cheers, -- Scott
[jQuery] Re: improve my working code
> I'm pretty new to jQuery and JS in general. I managed to > build something. I learned a lot! THX jQuery guys. When you > look into my code, could it be shorter, smarter, sharper? > especially this part: > [code] > $(document).ready(function(){ > $("#linkDetail1").click(function(){ > $("#detail1").slideToggle("slow"); > return false; >}); > > $("#linkDetail2").click(function(){ > $("#detail2").slideToggle("slow"); > return false; > }); > > $("#linkDetail3").click(function(){ > $("#detail3").slideToggle("slow"); > return false; > }); > } > [/code] > > what if I have to extend the IDs #linkDetail4, #linkDetail5, . One way to do it is with ordinary JavaScript loops and string concatenation. var maxDetail = 3; for( var i = 1; i <= maxDetail; ++i ) { $( '#linkDetail' + i ).bind( 'click', function() { $( '#detail' + i ).slideToggle( 'slow' ); }); } -Mike
[jQuery] Re: improve my working code
On 5/23/07, tlob <[EMAIL PROTECTED]> wrote: [code] $(document).ready(function(){ $("#linkDetail1").click(function(){ $("#detail1").slideToggle("slow"); return false; }); $("#linkDetail2").click(function(){ $("#detail2").slideToggle("slow"); return false; }); $("#linkDetail3").click(function(){ $("#detail3").slideToggle("slow"); return false; }); } [/code] $(["#linkDetail1", "#linkDetail2", "#linkDetail3"]).click(function(){ $(this).slideToggle("slow"); return false; }); what if I have to extend the IDs #linkDetail4, #linkDetail5, . You'll likely want to find a different selector in that case. Maybe give them each the same class and use $(".className").click(... - Richard D. Worth