javascript - How can I add new content to the contents of a variable and then replace that variable? -
my title might not worded , i'm sorry that. essentially, want done this:
newc = 'wow' jquery(".bleh").each(function(i){ var bluh = *this changes everytime statement run. lets call x1, x2 , on* var newc = bluh + newc });
suppose .each() function run twice (because there 2 elements class "bleh"). need newc
'x2x1wow'
.
in first time ran add value if bluh (x1) 'wow'
(initial value of newc)
second time adds new value of bluh (x2) 'x1wow'
(because value of newc now) return newc = x2x1wow
how achieve this?
you need create variable outside function, otherwise changes lost when function exits:
var newc = 'wow' jquery(".bleh").each(function(i) { var bluh = *this changes everytime statement run. lets call x1, x2 , on* newc = bluh + newc });
the problem in side each
callback variable newc
declared local variable resetted in every iteration. need modify closure variable newc
declared outside loop, can remove var
in callback function
demo: fiddle
Comments
Post a Comment