jquery - Possible to loop over each in coffeescript? -
i'm trying programmatically create javascript buttons toggle visibility on page (for snappy tag filtering). works 1 tag:
trigger = ".sales-focus-btn" target = ".sales-focus" jquery -> $(trigger).toggle -> $("#primary-content").find('.container').hide() $("#primary-content").find(target).show() , -> $("#primary-content").find('.container').show()
is possible similar in coffeescript, arrays, e.g.
trigger = [ ".sales-focus-btn" ".service-focus-btn" ".other-focus-btn" ... ] target = [ ... ]
is possible loop on , create toggle each type of tag?
update
yes it's possible. use form:
myfunction = (el) -> console.log el myfunction elem elem in array
of course it's possible:
content = $('#primary-content') container = content.find('.container') tags = [ '.sales-focus' '.service-focus' '.other-focus' ] $.each tags, (tag) -> target = content.find(tag) $(tag + "-btn").toggle -> container.hide() target.show() , -> container.show()
remember cache dom elements. alternatively use for tag in tags
instead of jquery.each tags, (tag) -> ...
:
for tag in tags -> target = content.find(tag) $(tag + "-btn").toggle -> container.hide() target.show() , -> container.show()
(the do ->
iife necessary keep each target
in scope, @epidemian pointed out)
Comments
Post a Comment