Transitioning from the Play mode to the Pause mode upon pressing the Set button

Is there a way to make the button switch from play to pause once the 'Set' button is clicked?

Clicking on 'Set' should change the SVG to display the pause button icon.

Snippet of Code: https://jsfiddle.net/192h0w85/195/

    (function iife() {
    "use strict";
    const player = document.getElementById("player");
    const button = document.getElementById("button");
    const value = document.getElementById("input");
    const sent = document.getElementById("sent");
    button.addEventListener("click", function() {
        this.classList.toggle('is-playing')
        if (player.paused) {
            player.play();
        } else {
            player.pause();
        }
    });
    sent.addEventListener("click", function() {
        player.volume = 1.0;
        player.src = value.value;
    });
}());

Answer №1

It appears that you have already added a button click event.

You can simply trigger that click event to change the icons.

Here is how your code will look:

(function iife() {
  "use strict";
  const player = document.getElementById("player");
  const button = document.getElementById("button");
  const value = document.getElementById("input");
  const sent = document.getElementById("sent");

  button.addEventListener("click", function() {
    this.classList.toggle('is-playing')
    if (player.paused) {
      player.play();
    } else {
      player.pause();

    }
  });

  sent.addEventListener("click", function() {
    player.volume = 1.0;
    player.src = value.value;
    button.click();
  });
}());

Note: button.click();

(function iife() {
  "use strict";
  const player = document.getElementById("player");
  const button = document.getElementById("button");
  const value = document.getElementById("input");
  const sent = document.getElementById("sent");

  button.addEventListener("click", function() {
    this.classList.toggle('is-playing')
    if (player.paused) {
      player.play();
    } else {
      player.pause();

    }
  });

  sent.addEventListener("click", function() {
    player.volume = 1.0;
    player.src = value.value;
    button.click();
  });
}());
body {
  background-color: black;
}

.btn {
  position: relative;
  width: 200px;
  height: 200px;
  cursor: pointer;
}

.btn .play,
.btn .pause {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
}

.play {
  fill: #000000;
  border: 3px solid blue;
  border-radius: 50%;
}

.play path {
  fill: blue;
}

.pause {
  fill: #000000;
  border: 3px solid blue;
  border-radius: 50%;
}

.pause path {
  fill: blue;
}

.pause {
  display: none;
}

.is-playing .play {
  display: none;
}

.is-playing .pause {
  display: block;
}

.label {
  font-size: 22px;
  color: blue;
  font-family: Tahoma;
}

.info {
  display: table-cell;
  white-space: nowrap;
}

.input {
  font-size: 22px;
  width: 200px;
  margin-top: 40px;
  background: black;
  color: blue;
}

.sent {
  font-size: 22px;
  background: black;
  color: blue;
  cursor: pointer;
  font-family: Tahoma;
}
<audio autoplay id="player"></audio>


<div id="button" class="btn">
  <svg class="play hide" width="200" height="200" viewBox="0 0 24 24">

<circle cx="12" cy="12" r="12"> </circle>
<path d="M9.846 16.86c-.467.303-.846.097-.846-.45V7.588c0-.551.38-.752.846-.45l6.91 4.48c.324.21.327.549 0 .761l-6.91 4.48z">
    </path></svg>
  <svg class="pause" width="200" height="200" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="12"> </circle>

<path d="M8 7.596c0-.33.277-.596.607-.596h1.786c.335 0 .607.267.607.596v8.808a.605.605 0 0 1-.607.596H8.607A.602.602 0 0 1 8 16.404V7.596zm5 0c0-.33.277-.596.607-.596h1.786c.335 0 .607.267.607.596v8.808a.605.605 0 0 1-.607.596h-1.786a.602.602 0 0 1-.607-.596V7.596z">
    </path></svg>
</div>
<div class="info">
  <label class="label" for="input">Text</label>
  <input class="input" id="input" type="text" name="someNameHere" value="http://hi5.1980s.fm/;" />
  <input class="sent" id="sent" type="submit" value="Set" />
</div>

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

Is it possible to make any object reactive within Vuex?

Seeking ways to enhance the sorting of normalized objects based on a relationship. Imagine having an application that requires organizing data in a Vuex store containing multiple normalized objects, like this: state: { worms: { 3: { id: 3, na ...

Looking to enhance the accessibility of a dynamically generated file by implementing the ability to expand and collapse sections as parent nodes

Currently working on a webpage where I am showcasing a testsuite file as the parent node and test cases within the testsuite file as child nodes. I have added checkboxes to both the parent and child nodes. Now, my goal is to include an ex ...

Javascript: A Fun Game of Questions and Answers

When using JavaScript exclusively, I have an array consisting of four questions, four correct answers, and four incorrect answers. The use of arrays is essential to maintain order in the data. As each question is displayed, a random number is generated by ...

Tool for tracking response time on basic Ajax-requested webpage

Looking for an easy way to use ajax to load content onto a page. I want the loaded content to wait before appearing on the page, but using jQuery's .delay() function didn't work as expected. Any suggestions on how to achieve this? Appreciate any ...

Chrome crashing due to toDataURL

Recently, I have been working on a Tile Renderer for three.js and so far, everything appears to be functioning correctly. The process is quite simple: a) It creates multiple cameras, b) Renders the scene using each camera c) Generates a 'toDataURL&a ...

Storing an image or video file to the disk of a NodeJS server and creating a reference in MongoDB

Seeking advice on the optimal method to store an image or video file on a NodeJS server and link it to MongoDB. Additionally, I need to enable streaming for the videos. Any recommendations would be greatly appreciated. Thank you! ...

Utilize JavaScript to include HTTP headers in image requests

In my JS/HTML5 Project with angularjs, I have implemented protection for my API by setting an authorization token in the HTTP header. Now, I am also looking to secure access to images stored on the server. While I know how to accomplish this on the server ...

Struggling to store information in a MongoDB database within my MEAN Stack project

After successfully creating a collection for user LOGIN/LOGOUT and managing to store and retrieve data using Express app and mongoose, I encountered an issue when trying to create another collection. Despite successfully creating the collection, the data w ...

Identify the significance within an array and employ the filter function to conceal the selected elements

I'm in the process of filtering a list of results. To do this, I have set up a ul list to display the results and checkboxes for selecting filter options. Each item in the ul list has associated data attributes. When a checkbox with value="4711" is c ...

Send a res.json response and retrieve it using res.render in a different router

Trying to retrieve a JSON from the route "/api/product" and display it using hbs on the "/product" route. The code seems to be working, but is it done correctly? The router in this case is: '/api/product' router.get('/', this.controll ...

Nested React Components in React Router Components

class AppRoutes extends Component { render () { return ( <Suspense fallback={<Spinner/>}> <Switch> <Route exact path="/" component={ HomePage } /> <Route exact path="/acti ...

Ways to ensure a for loop waits until an Async call is successful before proceeding

Hey there! Currently, I am working on creating a batch update for my local store using a for loop and asynchronous Ajax calls. However, I'm facing an issue where the loop continues running even before the Ajax call has successfully completed. Is ther ...

Is there a way to launch a browser in full screen mode using a command line interface?

Is there a clever way to launch a web browser in full screen mode from the command line? The browser's full screen API only activates in response to user interaction. I simply want to show data on a large monitor without any unnecessary elements lik ...

Stopping an HTML5 range input at a specific number: Tips and tricks

Does anyone have suggestions on how to use angularjs to stop a range slider at 75? I attempted it but it doesn't seem like the best approach. Any guidance would be appreciated. [EDIT for clarification after max=75 answer] Just to clarify, I want to di ...

The imported package in Node.js cannot be located

I'm encountering an issue when trying to deploy my project on the server. Everything runs smoothly on my PC with no import problems. Thank you for your assistance! Error Message: Error [ERR_MODULE_NOT_FOUND]: Module '/home/igor/backend/alina_edu ...

A quick guide on automatically checking checkboxes when the user's input in the "wall_amount" field goes over 3

I'm looking to have my program automatically check all checkboxes (specifically "Side 1, Side 2, Side 3 and Side 4") if the input for wall_amount is greater than 3. Is there a way to achieve this? I attempted lines 10-12 in JavaScript without success. ...

What is the best way to retrieve the identity or specifics of the item that was right-clicked within the context menu

My AngularJS populated table includes a link button. When I right-click on this button, I've created a specific context menu using jQuery that pops up. The issue arises when I try to retrieve the ID of the item I clicked on in the context menu (such a ...

Set the text field to be editable or readonly based on certain conditions

Is there a way to conditionally enable or disable an editable field (edit/read only)? I have tried using session role from the database to set conditions on the text field, but I am unsure of how to proceed... <?php include('session.php'); ?& ...

What is the process for generating an array of arrays in a React component?

I'm currently working on developing a word-guessing game similar to Wordle using React. The interesting twist in this game is that players have the ability to choose both the number of attempts allowed and the length of the word they are trying to gue ...

Building a JavaScript application with Node.js and MySQL to seamlessly interact with both offline and online databases

As I develop a JavaScript web-app, my plan is to utilize Node.js for connecting the app with an existing MySQL database. My initial question pertains to the structure of the Node code: should it be written in the same .js file as my application or kept se ...