One minor correction to the sample code below. $(this).attr('class') should be $(this).attr('className')
- Richard On Mon, May 12, 2008 at 7:22 AM, andrea varnier <[EMAIL PROTECTED]> wrote: > > On 12 Mag, 12:31, quirksmode <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I am dynamically generating html which looks like this: > > since you're generating the html dynamically, I'd suggest you do it > server side (i.e. a for loop). > if that is not possible, change <div id="wrapper"> with <div > class="wrapper">, if you need the "wrapper" string, or simply <div>, > because having the same id over the page is definitely a bad thing :) > the a solution could be: > > $('div').each(function(i){ > var $this = $(this); > var the_id = $this.attr('class'); > $this.attr('id', the_id + '_' + i) > }); > > this would be zero-based. change to (i+1) to have them starting with > wrapper_1. > without the class, shorter: > $('div').each(function(i){ > $(this).attr('id', 'wrapper_' + i) > }); > > if those div's are inside something else (let's assume a <div > id="container_div">, you can add a context to the first selector, to > speed up the process: > > $('div', '#container_div').each(function(i){ > var $this = $(this); > var the_id = $this.attr('class'); > $this.attr('id', the_id + '_' + i) > });