[jQuery] Re: Attribute renaming
I am trying to rename the ids and name attributes On Sep 26, 10:06 am, Erik Beeson [EMAIL PROTECTED] wrote: What are you trying to accomplish? --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? Thanks
[jQuery] Re: Attribute renaming
Rename them from what to what? To change the id of all selects, you can do: $('select').each(function() { var oldId = $(this).attr('id'); $(this).attr('id', oldId.substring(0, oldId.lastIndexOf('-'))); }); I'm still not sure what you actually want to do though. --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I am trying to rename the ids and name attributes On Sep 26, 10:06 am, Erik Beeson [EMAIL PROTECTED] wrote: What are you trying to accomplish? --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? Thanks
[jQuery] Re: Attribute renaming
Hi Erik, I am cloning a DIV container full of elements( selects, inputs and buttons), they all inherit the name of the original element, but to differentiate between all, I want to rename all the clones attributes sequentially, example div name= container input name=color input name=house /dv if I clone the whole container, the names should be: div name= container input name=color input name=house /dv div name= container1 input name=color1 input name=house1 /dv ans so forth Thanks On Sep 26, 12:57 pm, Erik Beeson [EMAIL PROTECTED] wrote: Rename them from what to what? To change the id of all selects, you can do: $('select').each(function() { var oldId = $(this).attr('id'); $(this).attr('id', oldId.substring(0, oldId.lastIndexOf('-'))); }); I'm still not sure what you actually want to do though. --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I am trying to rename the ids and name attributes On Sep 26, 10:06 am, Erik Beeson [EMAIL PROTECTED] wrote: What are you trying to accomplish? --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? Thanks
[jQuery] Re: Attribute renaming
How about something like (untested): $('#container').clone().attr('id', 'container'+idx).find('*').each(function() { this.name += idx; this.id += idx }).end().appendTo(...); How you deal with 'idx' is up to you. So that's: select the element with ID 'container', clone it, update the clone's id, select all of the clone's descendant elements, update their names and IDs, go back to the clone and append it to where ever you want it to show up. --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: Hi Erik, I am cloning a DIV container full of elements( selects, inputs and buttons), they all inherit the name of the original element, but to differentiate between all, I want to rename all the clones attributes sequentially, example div name= container input name=color input name=house /dv if I clone the whole container, the names should be: div name= container input name=color input name=house /dv div name= container1 input name=color1 input name=house1 /dv ans so forth Thanks On Sep 26, 12:57 pm, Erik Beeson [EMAIL PROTECTED] wrote: Rename them from what to what? To change the id of all selects, you can do: $('select').each(function() { var oldId = $(this).attr('id'); $(this).attr('id', oldId.substring(0, oldId.lastIndexOf('-'))); }); I'm still not sure what you actually want to do though. --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I am trying to rename the ids and name attributes On Sep 26, 10:06 am, Erik Beeson [EMAIL PROTECTED] wrote: What are you trying to accomplish? --Erik On 9/26/07, voltron [EMAIL PROTECTED] wrote: I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? Thanks
[jQuery] Re: Attribute renaming
I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? $('select).each(function() { id=$(this).attr('id'); //number after '-', useless if you dont need number number=id.substring(id.lastIndexOf('-')+1); //new value that you want to put after '-' newvalue='1234567'; changed=id.substring(0,lastIndexOf('-')+1)+newvalue; } or something like that (I didnt check that) :) Regards Michael
[jQuery] Re: Attribute renaming
Thanks guys, I used a mixture of both your suggestions: var ident = String(this.name); changed= ident.substring(0 ,ident.lastIndexOf('-')+1) + suffix; $(this).removeAttr(id); $(this).removeAttr(name); this.name += changed; this.id += changed; $(this).attr(clone, changed); On Sep 26, 11:06 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I have tags like this: select id=academic_from_month-0 select id=academic_till_month-0 since the ids are variable in length, I would have to find out where the lastindex() of - is remove that and replace it it with the new value. My problem is the syntax, is there some kind of Jquery way to do this? $('select).each(function() { id=$(this).attr('id'); //number after '-', useless if you dont need number number=id.substring(id.lastIndexOf('-')+1); //new value that you want to put after '-' newvalue='1234567'; changed=id.substring(0,lastIndexOf('-')+1)+newvalue; } or something like that (I didnt check that) :) Regards Michael