I have a function that adds a nested form when clicked, and it is working perfectly.
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
However, I need this function to also trigger once on page load and then continue to work when clicked. To achieve this, I added the following code:
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(document).on 'load', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
After adding the onload event as described above, the function doesn't trigger on page load. However, the click event still works correctly and adds the nested forms. How can I fix this issue?