Change the background of the play/stop icon with a jquery toggle

There is a list of video links with play icons as backgrounds in front of them. When a user clicks on a link, the video will start playing in a player located to the left of the links. The clicked link's background icon changes to a 'stop' icon while any previously playing videos revert back to their 'play' icons.

<ul id="playlist">
        <li><a class="play" href="http://video-js.zencoder.com/oceans-clip.mp4" onclick="playClip(this.href); return false;">Oceans</a>
        </li>
        <li><a class="play" href="http://html5videoformatconverter.com/data/images/happyfit2.mp4" onclick="playClip(this.href); return false;">Happy Fit</a>
        </li>
        <li><a class="play" href="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" onclick="playClip(this.href); return false;">Sintel</a>
        </li>
</ul>

My CSS:

#playlist a.play{
    background-image: url('/_layouts/images/plplay1.gif');
    background-repeat: no-repeat;
    padding-left: 30px;  
    display: block;
}

#playlist a.stop{
    background-image: url('/_layouts/images/plstop1.gif');
    background-repeat: no-repeat;
    padding-left: 30px;  
    display: block; 
}

And my JS:

$('#playlist li a').click(function() {
    if ($(this).hasClass('play')) {
        $(this).removeClass('play').addClass('stop');
    } else if ($(this).hasClass('stop')) {
        $(this).removeClass('stop').addClass('play');
    } else {
        $(this).addClass('play');
    }
});

Here is the fiddle for reference: http://jsfiddle.net/2Bsm7/

Answer №1

Here's a helpful suggestion:

When you click on any link inside the playlist, if it doesn't have the class 'stop', the code will toggle the classes 'play' and 'stop' for all links with the class 'stop'. It will then toggle the classes 'play' and 'stop' for the clicked link.

Check out the updated fiddle here.

Answer №2

Give this a shot:

  $('ul.playlist li a').on('click', function() {
     $('ul.playlist li a').removeClass('stop').addClass('play');
     $(this).addClass('stop');
   });

Check out the DEMO 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

Automatically populate textboxes with database information based on selected combobox item without the need to refresh the page

I would like to implement a feature where textboxes are automatically filled from a database when a user selects an option from a combo box. This can be achieved using jQuery or JavaScript in PHP. Once a user selects an element, the corresponding text sh ...

Updating HTML element classnames with React hooks

My goal is to remove an attribute from an HTML element when a button is clicked: import React , {useState} from 'react'; import classNames from 'classnames'; function App () { const [isActive, setIsActive] = useState(false); co ...

Is there a way for me to position my arrow beside the header while still keeping the toggle function intact?

Can anyone assist me in positioning my arrow next to the header text, which I toggle on click? I attempted placing the arrow <div> into the H1 tag, but then the toggle function stops working. Any help would be greatly appreciated! Please includ ...

What is the best way to prevent excessive rerenders when verifying if database information has been successfully retrieved?

Hey there, I'm encountering an issue where the if statement check in my code is causing a "too many rerenders" problem. I'm trying to create a delay between pulling data from the database and calculating the BMI. Any suggestions on how to resolve ...

The transition effect is not activated when using the MUI Drawer with react-router-dom

My goal was to merge the persistent drawer similar to the example on the MUI website with react-router-dom. However, I encountered a problem where the transition effect no longer seems to work properly. The paragraph does not resize to accommodate the open ...

Integrating jquery events and functions with angularjs

Recently, I've come across this piece of code: $('.btn').click(function() { $(this).removeClass('big-red-button'); $(this).addClass('btn-loading'); $(this).find('.text').text('Working..'); ...

Encountered an error: Trying to access the property 'push' of an undefined value

Hey there, I've successfully used jQuery to dynamically generate a textbox. Now, I am facing an issue where I need to retrieve the value entered in the textbox using an AJAX function and then pass that value to another PHP file. When I type something ...

Turning backbone's collection toJSON method into a collection object

One of the attributes of my model is a backbone collection. When I print out the model, everything appears to be fine, including the collection. However, when I use the toJSON() method on the collection and output it, the entire collection object is disp ...

Combining items from the identical array in pairs using distinct identifiers

I have an array containing double values. const data = [ -0.003,-8.178509172818559E-16, 0.002,-1.3576469946421737E-15, 0.007,-1.2329107629591376E-1] I am looking to utilize these values in react-vis. The necessary syntax for data insertion in react-vis ...

The XMLHttpRequest onload() function is failing to pass an instance of the XMLHttpRequest object

I am facing an issue with a function that sends a basic AJAX request to my server. The JavaScript code in the browser is as follows: function testRequest() { var xhr = new XMLHttpRequest(); xhr.onload = () => { console.log("RESPONSE R ...

What could be causing my AngularJs routing and animation to bypass the second redirection?

Recently started working with Angular and experimenting with routing and animations to manage my page transitions. I followed a helpful guide that helped me set everything up. I encountered a few issues: When trying to link back to the landing page (home ...

What's the best way to position a child fixed DIV in relation to a parent fixed div?

Here is the HTML snippet I am working with: <div class="parent"> <div class="child"></div> </div> The corresponding CSS styles are as follows: .parent { position: fixed; } .child { position: fixed; left: ...

bring in all the files located within the Directory

Is there a way to import all CSS files from a specific folder without having to import each file individually? Looking to import assets/css/* ? <link href="<?php echo base_url(); ?>assets/css/*" rel="stylesheet"/> <title&g ...

The function inputLocalFont.addEventListener does not exist and cannot be executed

Help needed! I created a code to add images using PHP and JS, but I'm encountering an error in my JS that says: inputLocalFont.addEventListener is not a function Here's the code snippet: <div> <img src="<?php echo $img_path.& ...

Organize a method's for-loop using JavaScript modules

I have a function that looks like this: function createModifiedList(people){ const modifiedList = [] for (let i = 0; i < people.length; i++){ modifiedList.push({ name: person.firstName + " " + person.lastName, ...

What steps can be taken to eliminate the 1-pixel line between the background and gradient?

How can I remove the line that appears between the block and the gradient background when zooming in, especially noticeable in Safari (After zooming a couple times, Cmd +) For more details, please refer to this jsfiddle link: http://jsfiddle.net/lokapujya ...

Display a div every 3 seconds with the help of jQuery

I am seeking a solution to display the second div (box2) every 3 seconds. Can anyone help me achieve this using jQuery? <div id="box1" style="background-color:#0000FF"> <h3>This is a heading in a div element</h3> <p>This ...

Access cookie information within a Vue application by reading it within the router or component

There is a url (*/webapp/callback) in my vue.js app that gets redirected (http 302) from another application, bringing along some cookies. I'm trying to figure out how I can read these cookies when the component mounts and then store them in vuex stat ...

Can you import folders containing *.vue files in your project?

Repeating code is my least favorite thing. Currently, I am importing vue files in a repetitive manner in my main.js file. import Default from '../../src/components/default.vue'; import Home from '../../src/components/home.vue&apo ...

React error: Unable to iterate through items because property 'forEach' is undefined

I am trying to implement private routing validation using the following code: import React from 'react'; import { Route, Redirect } from 'react-router-dom'; import routes from '../../routing/routes'; export default function ...