javascript - Passing parameters to Anonymous function inside click event -
var div_raw_id = 'acf-download-link'; var div_id = '#' + div_raw_id; var chapter_index = 1; var chapter = 'chapter-' + chapter_index; $('button[name=addnewchapter]').click(function(chapter, div_id ,div_raw_id){ $(div_id).append('<div class="acf-input-wrap"><input id="' + div_raw_id +'" class="text" name="fields[field_55951fb44c1d6][' + chapter + '][]" value="" placeholder="" type="text"><span class="remove_btn"></span></div>'); return false; });
the problem cannot pass div_id
, chapter_index
, chapter
parameter anonymous function inside click event. used debugger , debugger represents them undefined values though defined in above code. seems there variable scope problem, however, cannot figure out other way pass defined variables parameter anonymous function inside click event.
you don't need pass variables defined @ beginning parameters, use inside function:
var div_raw_id = 'acf-download-link'; var div_id = '#' + div_raw_id; var chapter_index = 1; var chapter = 'chapter-' + chapter_index; $('button[name=addnewchapter]').click(function(){ $(div_id).append('<div class="acf-input-wrap"><input id="' + div_raw_id +'" class="text" name="fields[field_55951fb44c1d6][' + chapter + '][]" value="" placeholder="" type="text"><span class="remove_btn"></span></div>'); return false; });
Comments
Post a Comment