Utilize page variables to activate audio clips upon clicking

Can someone assist me with a script that activates an audio clip when five icons on a page are clicked? The image of the icons can be found below:

https://i.stack.imgur.com/mBC6o.png

Here is the HTML code:

<div class="slide overlay-container" id="inter8">
    <div class="container content box-middle slide-back8">
        <audio data-autoplay>
            <source src="audio/mars_glm_intro_slide05a.mp3" type="audio/mp3">
        </audio>

        <div class="row zero">
            <div class="top txt-ctr">
                <div class="col-md-2 col-xs-2">
                    <img src="images/Icon.png" style="width:150px; height:93px;">
                </div>
            </div>
        </div>
        <!-- Icons and bars -->
        <div class="row zero">
            <div class="mdl txt-ctr hght txt-white">
                <div class="col-md-2 col-xs-2 mar"></div>

                <!-- Blue -->
                <div class="col-md-2 col-xs-2 icn1 btn1" tabindex="1">
                    <img src="images/Icon1.png" style="width:150px; height:93px;">
                </div>
                <div class="col-md-2 col-xs-2 blu mar">
                    Recruit and<br>
                    Onboard<br>
                    current and<br>
                    future Associates
                </div>
                <!-- Green -->
                <div class="col-md-2 col-xs-2 icn2 btn2" tabindex="1">
                    <img src="images/Icon2.png" style="width:150px; height:93px;">
                </div>
                <div class="col-md-2 col-xs-2 green mar">
                    Maximize<br>
                    Performance<br>
                    of Associates
                </div>
                <!-- Purple -->
                <div class="col-md-2 col-xs-2 icn3 btn3" tabindex="1">
                    <img src="images/Icon3.png" style="width:150px; height:93px;">
                </div>
                <div class="col-md-2 col-xs-2 purple mar">
                    Engage<br>
                    Associates
                </div>
                <!-- Orange -->
                <div class="col-md-2 col-xs-2 icn4 btn4" tabindex="1">
                    <img src="images/Icon4.png" style="width:150px; height:93px;">
                </div>
                <div class="col-md-2 col-xs-2 orange mar">
                    Develop<br>
                    Associates
                </div>
                <!-- Yellow -->
                <div class="col-md-2 col-xs-2 icn5 btn5" tabindex="1">
                    <div class="play">
                        <img src="images/Icon5.png" style="width:150px; height:93px;">
                    </div>
                </div>
                <div class="col-md-2 col-xs-2 yellow mar">
                    All in the<br>
                    Mars Way
                </div>
            </div>
        </div>
        <div class="row page8-btm zero">
            <div class="btm">
                <h2>What is Great Line Management?</h2>
                <p>Click the icons to learn more.</p>
            </div>
        </div>
    </div>
</div> 

Below is the JavaScript code for the script:

var click1 = false;
var click2 = false;
var click3 = false;
var click4 = false;
var click5 = false;

$('.btnClass').click(checkProg);

function checkProg(){
    var thisBtn = $(this).attr('id');

    if(thisBtn == "btn1") {
        click1 = true;
    } else if (thisBtn == "btn2") {
        click2 = true;
    } else if (thisBtn == "btn3") {
        click3 = true;
    } else if (thisBtn == "btn4") {
        click4 = true;
    } else if (thisBtn == "btn5") {
        click5 = true;
    }

    if(click1 == true && click2 == true && click3 ==  true && click4 ==  true && click5 ==  true) {
        'audio/mars_glm_intro_slide05b.mp3'
    }
}

I'm not receiving any errors from the script, but it's not functioning as expected. Any assistance would be greatly appreciated.

Answer №1

Your code has a couple of issues that need to be addressed:

1) The processing for elements with the .btnClass class is incorrect because none of the elements in your code have this class assigned to them.

2) You are checking the ID of the clicked element and looking for the attribute ID, but the btnX value is actually added as a class, not an ID.

To fix these issues, use the following updated code snippet:

<div class="slide overlay-container" id="inter8">
        <div class="container content box-middle slide-back8">
        <audio data-autoplay>
        <source src="audio/mars_glm_intro_slide05a.mp3" type="audio/mp3">
    </audio>

            <div class="row zero">
                <div class="top txt-ctr">
                   <div class="col-md-2 col-xs-2"><img src="images/Icon.png" style="width:150px; height:93px;">
                   </div>
                </div>
            </div>
            <!-- Icons and bars -->   
            <div class="row zero"><div class="mdl txt-ctr hght txt-white">
                <div class="col-md-2 col-xs-2 mar">

                </div>

               <!-- Blue --> 
                <div class="col-md-2 col-xs-2 icn1" tabindex="1">                 <img id="btn1"  src="images/Icon1.png" style="width:150px; height:93px;" class="imgClicker">
                </div>
                <div class="col-md-2 col-xs-2 blu mar">Recruit and<br>
                    Onboard<br>
                    current and<br>
                    future Associates
                </div>
                <!-- Green -->  
                <div class="col-md-2 col-xs-2 icn2" tabindex="1">   <img id="btn2" src="images/Icon2.png" style="width:150px; height:93px;" class="imgClicker">
                </div>
                <div class="col-md-2 col-xs-2 green mar">Maximize<br>
                    Performance<br>
                    of Associates
                </div>
                <!-- Purple -->
                <div class="col-md-2 col-xs-2 icn3" tabindex="1"><img id="btn3" src="images/Icon3.png" style="width:150px; height:93px;" class="imgClicker">
                </div>
                <div class="col-md-2 col-xs-2 purple mar">Engage<br>
                    Associates
                </div>
                <!-- Orange -->
                <div class="col-md-2 col-xs-2 icn4" tabindex="1"><img id="btn4" src="images/Icon4.png" style="width:150px; height:93px;" class="imgClicker">
                </div>
                <div class="col-md-2 col-xs-2 orange mar">Develop<br>
                    Associates
                </div>
                <!-- Yellow -->
                <div class="col-md-2 col-xs-2 icn5" tabindex="1"><div class="play"><img id="btn5" src="images/Icon5.png" style="width:150px; height:93px;" class="imgClicker"></div>
                </div>
                <div class="col-md-2 col-xs-2 yellow mar">All in the<br>
                    Mars Way
                </div>
                </div>
            </div>
            <div class="row page8-btm zero"><div class="btm">
                <h2>What is Great Line Management?</h2>
                <p>Click the icons to learn more.</p></div>

            </div>
        </div>
    </div>   

Check out the JavaScript code below to handle click events on the icons:

 var click1 = false;
    var click2 = false;
    var click3 = false;
    var click4 = false;
    var click5 = false;

     $('.imgClicker').click(checkProg);

     function checkProg(){
     var thisBtn = $(this).attr('id');

     if(thisBtn == "btn1"){
      click1 = true;
     }else if (thisBtn == "btn2"){
     click2 = true;
     }else if (thisBtn == "btn3"){
     click3 = true;
     }else if (thisBtn == "btn4"){
     click4 = true;
     }else if (thisBtn == "btn5"){
     click5 = true;
     }

     if(click1 == true && click2 == true && click3 ==  true && click4 ==  true && click5 ==  true){
     alert('Play that song!');
     var audioElement = document.createElement('audio');
     audioElement.setAttribute('src', 'https://www.soundjay.com/misc/sounds/bell-ringing-01.mp3');
     audioElement.play();


     }

 }

You can also view a working Fiddle here

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

Error message consistently pops up when using the jQuery search feature

My jQuery function is fetching data from a different domain, and I want to use AJAX to display that data when the user enters a value into the search box. However, no matter if I search by .productName or .itemCode, the app always gives me an error messa ...

Activate the Bootstrap Jquery/Ajax inline editing feature by simply clicking on the Edit button

Seeking recommendations for a way to implement inline editing. When the edit button is clicked, I want the label's content to be replaced with an input text field that can be updated in my MySQL database. This is what my code looks like: <label s ...

Prevent the loading of a <img> element and replace it with a new one using CSS

Is there a way to hide an <img> element without accessing the HTML directly, only by using CSS? The <img> element does not have any specific class. Here is the HTML markup: <img title="Manual" id="imgAjuda" src="images/ico_aj.png" width=" ...

Replace anchor text using jQuery

I'm currently working on implementing a like system, but I've encountered an issue. When a user clicks the like button, the text should change from "Like" to "Remove Like". The data is being fetched from a PHP query, so there are multiple items ...

What is the method for adding 24 hours to a 12-hour timestamp while preserving the AM and PM designation?

I have created the following code to display real-time, but I am struggling with adding a timestamp that switches from 24-hour format to 12-hour format with AM and PM. setInterval(function() { var date = new Date(); var hours = date.getHours(); va ...

Crop images in a canvas using a customized rectangle with the help of JQuery

I am trying to crop an image inside a Canvas element using a selection rectangle. My project utilizes jQuery and I am in search of a plugin that can help me implement this custom selection rectangle on the canvas itself, not just on images. Are there any ...

Issue with Hiding Bootstrap Tabbed Content Correctly

I am currently struggling to make a Bootstrap tab field function correctly, encountering some obstacles along the way. While the content is successfully tabbing between each div, the issue I'm facing is that the content from each tab is actually bein ...

The columns in CSS grid-template are refusing to change despite the media query code being applied and active

Currently, I am facing an issue while working on a site plugin and trying to utilize CSS grid to showcase posts. Despite the media query being active according to the inspector, the modification in the template columns is not reflecting as expected. Check ...

What may be causing the MuiThemeProvider to override the style of a component?

Within my outer component, I am utilizing MuiThemeProvider: <MuiThemeProvider theme={full_theme_e}> <div> <AppBar /> <Filter /> </div> </MuiThemeProvider> Inside the Filter component, I have specified a ...

Why is it not possible for me to place my image and text side by side?

I've been working on my personal portfolio website and ran into an issue when trying to position the image with text that says Ib student. I attempted using the bootstrap box model which didn't work, followed by using float, but that also didn&ap ...

Strategies for avoiding overflow-x issues in Safari and mobile devices

I am currently facing an issue where I have a div containing a table with overflow enabled, allowing the whole table to be viewed by scrolling. However, on Safari and mobile Safari browsers, there is a horizontal scroll on the html and body elements. Whil ...

Display an AJAX div when the image is hovered over and have it track the movement of

I am seeking assistance with my website project, which involves providing users with download links to movies. However, I am struggling to make the preview_block div(id) appear when the mouse hovers over the movie_block div(id). Additionally, I am having ...

Complete the submission by either clicking the mouse or pressing the enter key

When submitting a form, I can do so by clicking with the mouse or pressing ENTER. An AJAX call is made to check if a designation already exists in the database. If it doesn't exist, the user can submit the form. Otherwise, the SUBMIT button will be d ...

Tips for implementing automatic line wrapping in a PDF document using React-PDF

I am experiencing an issue with a PDF rendering where very long words are not wrapping onto a new line, instead overflowing the container and disappearing off the page. Below is the setup causing this problem. The styles are listed followed by the actual ...

Search for text in multiple tables using jQuery and automatically trigger a button click event when the text is found

I am attempting to query certain tables and click a button within a cell of a specific table. Below is the code I am currently using: links[1].click(); iimPlayCode('WAIT SECONDS = 2') var compTabs = window.content.document.getElementById(' ...

The jQuery newsfeed is powered by the $.each() method

I'm having trouble setting up a newsfeed on my website. It's supposed to display all the current news and exclude any expired ones. I have a webpage where I can add news, so the JSON data is constantly changing. Here's an example of the PHP ...

Exploring the Transition from React.js with Material UI to Next.js for Server-Side Rendering: A Comparison of Tailwind CSS, Material UI, and Chakra UI

Currently, I am in the process of moving my React.js application with Material UI components to Next.js for server-side rendering (SSR) while considering some UI changes. In my research, I have come across three UI frameworks: Material UI, Chakra UI, and T ...

Using Jquery to adjust the width of table cells within a div container

Is there a way to use JQuery to access the first table's td in this HTML code and change the width to 40% and 60% respectively? <div id="DeltaPlaceHolderMain"> <a id="mainContent" tabindex="-1" name="mainContent"></a> ...

Every attempt to connect with the webservice via an ajax call has ended in failure

I am encountering an issue with a webservice call that I am making using jQuery's AJAX method. Despite receiving JSON data from the webservice when accessed directly through the browser, my site always triggers the error function rather than the succe ...

Preventing Credit Card Charges with Stripe in Laravel 4: Waiting for Another Form Submission

Currently, I am in the process of creating a form that will include a Stripe payment button. The layout is expected to resemble the following (consider the finish button as faded out with all necessary information yet to be filled in): My intention is for ...