I’m trying to make a very basic thing in CoffeeScript:
...
nextId = $('.optionsTemplate').prev().children().length + 1
# 'alert nextId' gives 4, which is correct
...
newOption = /*selector of new option*/
newOption.attr 'id', 'item_options_' + nextId
newOption.attr 'name', 'item[options]['+ nextId +']'
So when I call “nextId” (when setting id and name) – JS console says “nextId is not a function”
I tried a couple of things:
# get text instead of integer
nextId.text()
# make a function
getNextId = () ->
$('.optionsTemplate').prev().children().length + 1
Getting same error.
Here’s compiled output:
$('.addOption').on('click', function() {
var contents, newOption, nextId;
nextId = $('.optionsTemplate').prev().children().length + 1;
contents = "<div class='row'>" + $('.optionsTemplate').html() + '</div>';
$(this).before(contents);
newOption = $('.optionsTemplate').prev().children().eq(-1).find('.text_field');
newOption.attr('id', 'item_options_' + nextId);
return newOption.attr('name', 'item[options][' + nextId(+']'));
});
Seems Ok to me.
Change above to