views:

81

answers:

3
    clone.find('[id]').each(function() {
        id = $(this).attr('id');
        ind = id.search(/\d+$/);
        $(this).attr('id', id.substr(0,ind)+id_counter);
    });
    clone.find('[for]').each(function() {
        id = $(this).attr('for');
        ind = id.search(/\d+$/);
        $(this).attr('for', id.substr(0,ind)+id_counter);
    });

I know I can find elements that have either the id attribute or the for attribute, but then how do I know which one I need to set?

+2  A: 
var list = ['id', 'for']
for (var i in list) {
  var v = list[i];
  clone.find('[' + v + ']').each(function() {
    id = $(this).attr(v);
    ind = id.search(/\d+$/);
    $(this).attr(v, id.substr(0,ind)+id_counter);
  });
}
delete list;
SHiNKiROU
Uhh ... there's no `foreach` keyword in Javascript. It's a pretty bad idea to write `for` loops with "in" like that, because there might be other things on that array object that the loop would trip over.
Pointy
... well ok there's `for each` in new Javascript but it means something different and it's really not for arrays anyway.
Pointy
+3  A: 

If all you're trying to do is reduce code, you could...

 function doTheseThings(element) {
    clone.find(element).each(function() {
       var id = $(this).attr('id');
       var ind = id.search(/\d+$/);
       $(this).attr('id', id.substr(0,ind)+id_counter);
    });
 }

 doTheseThings('[id]');
 doTheseThings('[for]');

EVEN BETTER:

I decided to meld my first answer with some of @SHiNKiROU's ideas and add some more Jquery-ish syntax:

var items = ['[id]', '[for]'];
$.each(items, function (index, element) {
    clone.find(element).each(function() {
       var id = $(this).attr('id');
       var ind = id.search(/\d+$/);
       $(this).attr('id', id.substr(0,ind)+id_counter);
    });
});
Byron Sommardahl
Added `var` on @icktoofay's recommendation.
Byron Sommardahl
You can't do "array.each()" with jQuery; that's a Prototype thing. You're thinking of `$.each(['id', 'for'], function() { ... })`
Pointy
On Pointy's recommendation, I fixed the code. I have seen the error of my ways. :)
Byron Sommardahl
ok! looks fine now
Pointy
You need to change the parameters in both calls to `attr` as well.
interjay
Yeah.... shinkirou gave me the idea of using `$.each` too. Works great. I thought about moving it into a function, but I knew there had to be a way without one ;)
Mark
+1  A: 

Well, the redundency is in that the same code applies to the different attributes, so you factor those out:

$.each(['id', 'for'], function (ignr, attr) {
  clone.find('['+attr+']').each(function() {
      val = $(this).attr(attr);
      ind = val.search(/\d+$/);
      $(this).attr(attr, val.substr(0,ind)+id_counter);
  });
});

Then, if you want, you could remove a local variable to shorten it, but I'm not sure this is an improvement:

$.each(['id', 'for'], function (ignr, attr) {
  clone.find('['+attr+']').each(function() {
      val = $(this).attr(attr);
      $(this).attr(attr, val.substr(0,val.search(/\d+$/))+id_counter);
  });
});
ndp