Form triggering in javascript event

I have a good understanding of rubies and rails, but I lack knowledge in javascript. What I would like to do is track when the user finishes watching the video.

I am using wistia and my code currently looks like this:

:javascript
  wistiaEmbed = Wistia.embed("#{@chapter.video_id}", {
    autoPlay: true,
    videoFoam: true,
    playerColor: '000000'
  });
  wistiaEmbed.bind("end", function () {
    document.getElementById('target').style.display = 'none';
    document.getElementById('finished').style.display = 'block';
  });

      

What I want to accomplish is call the create action on my chapter_completion model. This takes a user_id and chapter_id at the end of the file.

So I could make this work in the form by doing:

form_for current_user.chapter_completions.new do |f|
 f.hidden_field :chapter_id, value: @chapter.id
 f.submit
end

      

However, I want this to be sent automatically in the background at the end of the wistia video.

+3


source to share


1 answer


 wistiaEmbed.bind("end", function () {
    document.getElementById('target').style.display = 'none';
    document.getElementById('finished').style.display = 'block';
    document.getElementById('formID').submit(); // or
    //document.getElementsByName('formName').submit();
  });

      



0


source







All Articles