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

Using CSS3 animation when adding a class, rather than on page load

I am trying to trigger my keyframe animations when the .show class for the popup window is added through JS, but currently the animation only activates upon page load. This is the JavaScript code I have: <script type="text/javascript"> $(document) ...

How can PHP Ajax be used to determine when a modal should pop up?

Is there a way to automatically display a modal without refreshing the page? Currently, I have a button that submits to home.php and triggers the modal, but it requires a page refresh for the modal to appear. I'm looking for a solution that will eith ...

The Jquery treeview plugin allows users to easily create

For my application, I am utilizing jquery.bassistance.de/treeview/. Is there a way to configure the nodes to only expand when clicking on the '+' icon? Currently, it expands even when clicking on the text following the '+'. ...

Tips for customizing the md-select icon

I work with the angular-material framework and specifically utilize its <md-select/> component. I am interested in styling its icon: https://i.stack.imgur.com/g4pu1.png In the past, we would modify its css by targeting the class .md-select-icon, ...

Why does the <select> dropdown flash when I select it?

Currently utilizing Angular 1.3 and Bootstrap 3.3.x CSS without the JS functionality. There is also an interesting animated GIF embedded within. <div class="form-group"> <div class="col-lg-3"> <label clas ...

Achieving text alignment with icons in Angular

I'm having trouble aligning my text with the icon next to it despite trying to adjust margins. I would really appreciate any help or suggestions on how to resolve this issue. <div class="notification" [ngClass]="{'no ...

Issue with Lightbox iFrame

There is a challenge I am facing with an iFrame that contains a lightbox. My goal is to redirect the parent page to a different web address once a link in the lightbox pop-up is clicked. Does this scenario sound clear? I would greatly appreciate any assist ...

When the button is clicked, a modal will pop up

Looking for help in incorporating JavaScript to create a responsive modal that pops up with lyrics when a button is pressed in a table. Any assistance would be greatly appreciated. Note: Only the code for the table has been provided. Let me know if you&ap ...

What methods are available to keep a component in a fixed position on the window during certain scrolling intervals?

I need help creating a sidebar similar to the one on this Airbnb page. Does anyone have suggestions for how to make a component stay fixed until you scroll past a certain section of the page? I am working with functional React and Material-UI components, ...

Sorting the Czech alphabet with the help of Tablesorter

Utilizing the tablesorter characterEquivalents extension as outlined in this documentation. I have created a similar extension for Czech characters, but the sorting is not functioning correctly for certain characters - like \u017d $.extend( $.tables ...

Utilize GroupBy in JavaScript to categorize JSON data and display it within an optgroup

I seem to be a bit disoriented. I have received this JSON data: [{ "id": "210", "name": "Name 1", "category": "Category 1" }, { "id": "187", "name": "Name 2", "category": "Category 1" }, { "id": "186", "name": "Name 3", ...

Utilizing Next.js - Implementation of a unique useThemeMode hook to efficiently manage theme preferences stored in the browser's localStorage, seamlessly integrating with toggleTheme

For a while now, I've been trying to figure out how to toggle my favicon based on the theme logic of my application and the current theme state stored in localStorage. My approach involves using CSS variables and data attributes applied to the html bo ...

Exploring the Spotify API with jQuery/ajax to fetch details about songs, artists, and albums

I've been researching and have come across some information that is somewhat similar, but I'm still struggling to completely understand how this process works. My goal is to make a request to the Spotify API using jQuery to search for an artist ...

Guide on populating a Kendo grid using a specific property from the server's JSON response, rather than the entire object

Here is the setup for my custom kendo grid: $("#grid").kendoGrid({ dataSource: { transport: { url: "customers", type: "GET", ...

C#: Issues with loading static content in MVC on various environments with IIS

Within my C# MVC 4.5 Website, I recently introduced a new folder to host CMS-type applications separately from the main site. While everything seemed fine during local testing, issues arose after deploying the updates to the DEV environment. A closer look ...

Utilizing version 4 of Bootstrap to create a visually appealing full

I am currently using the latest version of bootstrap, but I have encountered an issue with my sidebar being too short and not reaching full height. You can view the problem here: Is this problem caused by my CSS or is it a bootstrap issue? And how can ...

Using the window.setInterval() method to add jQuery/AJAX scripts to elements at regular intervals of 60 seconds

I'm having trouble with automatically updating a div. I have a script that refreshes the page content (similar to Facebook) every minute. The issue is that this div is newly added to the page and contains some ajax/jQuery elements for effects. functi ...

Error arising from CSS positioning with dynamically generated images

My challenge is displaying dynamic images from a database alongside details like color, name, and size, with a hover effect that shows a border. Whenever I hover over one image, the other images shift to the right or left. I want them to stay in place. T ...

Connecting two fields with a line on click in AngularJS

In the top section, there are 10 fields and in the bottom section, there are another 10 fields. I want to be able to connect two fields with a line when they are clicked (one from the top section and one from the bottom section). This connection should app ...

How to use ajax() to return multiple arrays successfully in jQuery

Hey everyone, I'm working on an AJAX function in jQuery that parses an XML file, creates an array on success, and wants to return multiple arrays on callback. Is it possible? If so, how can I achieve this? Please guide me through this. var arr1 = new ...