gastngouron gastngouron - 1 year ago 93
CoffeeScript Question

jQuery simple each/while loop in coffeescript does not work as expected

I've been trying couple of approaches on this problem. Basically I have to modify the content of several dropdowns on the exact same way. So I though I could use an each or a while to be more DRY but I dont have that much experience in coffeeScript.

Here is 'working' undry code to refactor:


Here is my first attempt:

arr = ['someId', 'someOtherId']
$.each arr, (i, el) =>

Here is my second attempt:

arr = ['someId', 'someOtherId']
length = arr.length
counter = 0
while counter < length
$('#' + arr[counter]).val('someVal')
counter ++

For some reasons, I have been trying multiple variations on each approch, I can assert that I can console.log the selector and it returns the correct value, but when it comes to change the value, the code just seems not to work as expected.

Any help is appreciated.

Answer Source


arr = ['someId', 'someOtherId']
for value, index in arr

Or if you don't care about index just:

arr = ['someId', 'someOtherId']
for value in arr
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download