What is the best way to stop an embedded mp4 video from playing when a dynamically generated button is clicked?

After making modifications to the QuickQuiz code found at https://github.com/UrbanInstitute/quick-quiz, I have replaced the img with an embedded mp4 video that loads from a JSON file. Additionally, I switched from using the original SweetAlert library to SweetAlert2. My goal now is to pause the playback of each video when any one of the four quiz buttons, identified by the class "quiz-btn btn", is clicked. These buttons trigger the SweetAlert2 dialog using swal.Fire.

I attempted to include scripts on the page in order to achieve this:

document.querySelector("#quiz > div > div.item.active > div.quiz-answers > div:nth-child(1)").addEventListener("click",function() {
document.querySelector("#quiz > div > div.item.active > div.ncc.text-center > video").pause();});

I also tried:

$('.quiz-button btn').on('click', function() {
        $('#video').attr('src', ''); });

I added these directly into the code but only managed to stop the first video from playing when the buttons were clicked. Unfortunately, the subsequent videos did not stop playing. Any assistance would be greatly appreciated. Thank you in advance.

Answer №1

This issue is quite prevalent, the solution involves updating your click function in the following manner:

Modify

$('.quiz-button btn').on('click', function() {

to

$(document).on('click','.quiz-button btn', function() {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance your Vue.js 3 tables with sorting, filtering, and pagination capabilities using this custom component

After extensive research, I am still unable to find an updated Vue.js 3 component that offers sorting, filtering, and pagination for tables without the need for additional requests to a backend API. The options I have come across either seem outdated, are ...

Learn how to retrieve the TextBox value from a button click within a Nested Gridview

I am trying to extract the value of a textbox inside a Nested Gridview using jQuery in ASP.NET. When a Button within the Nested Gridview is clicked, I want to display the textbox value in an alert box. Here is an example setup: <asp:GridView ID="Grid ...

Incrementing values in ng-repeat object automatically

My project involves extracting game information from mlb.com and utilizing angularjs along with the ng-repeat directive to display it. A sample of the JSON feed is shown below. { "data": { "games": { "next_day_date": "2017-08-19", "mo ...

Having trouble getting smooth scrolling with easing to function properly

I'm facing an issue with a JQuery function that is supposed to enable smooth scrolling using JQuery easing, but for some reason, it's not functioning correctly and I'm unable to pinpoint the error. Here is the code for the function: $(func ...

Transmitting a variable string from JavaScript to PHP through AJAX

How can I pass a variable value from an HTML page using JavaScript to PHP? In my index.php file, I have the following code: $amount = $_GET['pricenumb']; echo $amount; Here is the JavaScript code I used to call on click of a button and send th ...

Enabling scrolling for a designated section of the website

Example Link to Plunker <clr-main-container> <div class="content-container"> <div class="content-area"> <div class="row"> <div class="col-xs-9" style="height:100%; border-right: 1px solid rgb(204, 204, 204); padding-ri ...

Outputting an object using console.log in Node.js

When I print the error object in nodejs, the result of console.log(err) appears as follows: { [error: column "pkvalue" does not exist] name: 'error', length: 96, severity: 'ERROR'} I'm curious about the information enclosed ...

The Express application seems to load forever after a certain period of time

I encountered a peculiar problem with my express application. It was deployed on the server and functioning properly, but after a few hours, when I tried to access the website again, it kept loading indefinitely. Below is my app.js code: const express = r ...

What is the difference between using || (or) and defining a variable in JavaScript?

Similar Inquiry: What is the meaning of the expression (x = x || y)? On numerous occasions, I have noticed variables being assigned in this manner var d = d || {} or var = var || [] This has raised a few questions in my mind What is the main pu ...

Issue with current time malfunctioning

For my latest project, I've been tasked with creating a karaoke application using javascript. The challenge is to synchronize the lyrics with the song as it plays. Here's a snippet of my HTML code: <div id="lyric"></div> <audio i ...

What is the best method for extracting attribute values from multiple child elements using puppeteer?

When using this code, I can retrieve the attribute value of the first element selected. By adding /html/body/section/div[3]/img<2> or img<3> in the xpath, I am able to retrieve data for subsequent img elements. However, the parent element on t ...

Leveraging Node.js alongside a Spring backend

As I delve into a project involving React on the frontend and Spring on the backend (both running on the same machine), an interesting question arises. Given that Spring backend operates independently of Node, and the web browser is used to showcase the Re ...

What could be causing the empty object return from the Async function in my Typescript code on Next JS?

Encountering issues with an async function. In the ../lib folder, I have a class for handling data from an API website. However, when attempting to load the API data within an async function, I encounter difficulties. The async function does not return a ...

Challenges of aligning a modal overlay in the middle of mobile screens

Currently, I am developing a website and encountering a specific issue with the modal structure. When viewing it on Codepen using Chrome devtools and toggling the device toolbar to simulate mobile screens, everything appears fine. However, when opening the ...

Ensure that the callback response in the $.ajax() function is treated as JSON dataType

My code snippet: <script> $('#email').on('blur', function(){ email = $(tihs).val(); $.ajax({ type: "POST", url: "ajax.php", data: { 'email': email, ...

The presence of a default value within an Angular ControlValueAccessor triggers the dirty state due to

My task is to create dynamic Input components for a template driven form using a directive. The default value of the Input component should be set by the component itself. However, I encountered an issue where setting a default value automatically marks t ...

What is the best way to determine if this specific radio button has been selected?

I have been searching for solutions on stackoverflow, but haven't found anything helpful :( Currently, I am facing an issue with my HTML table that is loaded through an ajax request: <table class="table table-striped table-hover choix_annonce_tab ...

In JavaScript, loop through an array of arrays and combine them using the concat

If I have an array like [["a", "b"], ["c", "d"]], is there a way to iterate, reduce, map, or join this array in order to get the desired output of ["ac", "ad", "bc", "bd"]? What if the array is structured as [["a", "b"], ["c", "d"], ["e", "f"]]; how can we ...

ReactJS: What is the best way to rearrange elements within an array stored in an object's property?

I am attempting to swap the positions of two elements within an array, which is nested inside an object as a property. However, when I click on the second "^" button in my CodeSandbox example below, I encounter the error message TypeError: arr.container.ma ...

Can someone explain the meaning of this code?

Recently, I came across a project where this line of code was used in a jQuery script. I'm not sure of its purpose and would appreciate some help understanding why it was included. If necessary, I can provide the entire function for reference. $("#ta ...