[jQuery] Anyone have any ideas on why this code doesn't increment properly?
I've got this jQuery:
$(document).ready(function() {
$('#add-image').click(function() {
$('#image-div-1').clone(true)
.attr('id', function() {
return this.id.replace(/(.+)(\d+$)/, function(s, p1, p2) {
return p1 + (parseInt(p2, 10) + 1);
})
})
.appendTo('#image-input');
$('#main-photo-next:last').attr('name', function() {
return $(this).attr('name').replace(/(.+)(\d+$)/, function(s, p1, p2) {
return p1 + (parseInt(p2, 10) + 1);
})
});
return false;
});
});
And it's cloning the div (image-div-1) correctly, but the last part, $('#main-photo-next:last')
etc.,
is now renumbering the original name attribute of the first #main-photo-next div, instead
of the second (which would be the last when the first clone is created).
So I end up with <select id="main-photo-next" name="main-photo-2"> followed by
<select id="main-photo-next" name="main-photo-1"> instead of
<select id="main-photo-next" name="main-photo-1"> followed by
<select id="main-photo-next" name="main-photo-2">
Is the functioning order of the code wrong somewhere?
Rick
>
> > -----Original Message-----
> > From: jquery-en@googlegroups.com [mailto:jquery-en@googlegroups.com] On Behalf Of Ricardo Tomasi
> > Sent: Monday, December 29, 2008 11:02 AM
> > To: jQuery (English)
> > Subject: [jQuery] Re: Why isn't the name attr in this code incrementing?
> >
> >
> > ('#main-photo-next :last').name
> >
> > You're missing the $/jQuery and trying to get a property that doesn't
> > exist.
> >
> > change that to $(this).attr('name').replace(...) and you're set.
> >
> > On Dec 29, 1:36 am, "Rick Faircloth" <R...@WhiteStoneMedia.com>