Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .each on dynamic objects in jQuery?

There are lots of questions that seem to be asking this, but in the end all they want is to attach .click events and not .each and so the generally accepted answer in all of them include $("body").on("click", ".selector", function(){}); and this is definitely not what I want.

I have some generated inputs and I need to change the value of each one at the same time. Normally I would $(".inputs").each(function(){$(this).val("new-value")}; or something along those lines, however, I can't because of the dynamic aspect.

So how would you do a $("body").on("each", ".inputs", function(){});?

like image 316
o_O Avatar asked Jul 03 '13 15:07

o_O


2 Answers

Actually, it's as simple as running the .each inside setTimout.

setTimeout(function(){
  $(".inputs").each(function(){$(this).val("new-value")});
}, 5000);
like image 83
o_O Avatar answered Oct 15 '22 08:10

o_O


$.each works with any new elements added.

http://jsfiddle.net/4SV7n/

Doing something different each time:

http://jsfiddle.net/4SV7n/1/

like image 5
Samuel Reid Avatar answered Oct 15 '22 10:10

Samuel Reid