How to Identify the Correct File Type of an Image Using JavaScript?

I am trying to determine the file type of an image using JavaScript, but the code I have tried is displaying the file type as ‘jpg’. However, when I pass the same file to an external tool like https://exifinfo.org/detail/RjmJqqj8LGEaqOLBEaiyiw, it displays the file type as ‘webp’, which is the correct file type.
This is the code:

<!DOCTYPE html>
<html>
  <head>
    <title>Identify Image File Type</title>
  </head>
  <body>
    <img id="image" src="https://s.alicdn.com/@sc04/kf/H7c1b38f112d044889f7b2c5a2ab914ac3.jpg_100x100xz.jpg">
    <script>
      const image = document.getElementById('image');
      image.addEventListener('load', () => {
        const width = image.naturalWidth;
        const height = image.naturalHeight;
        const url = new URL(image.src);
        const extension = url.pathname.split('.').pop();
        console.log(`The file type is ${extension}`);
      });
      image.addEventListener('error', () => {
        console.log('The file is not a valid image');
      });
    </script>
  </body>
</html>

I tried using a third-party library, ‘file-type’ to determine correct file type. Here is the code I tried:

<!DOCTYPE html>
<html>
  <head>
    <title>Identify Image File Type</title>
  </head>
  <body>
    <img id="image">
    <script src="https://cdn.jsdelivr.net/npm/file-type/dist/index.umd.js"></script>
    <script>
      const imageUrl = 'https://s.alicdn.com/@sc04/kf/H7c1b38f112d044889f7b2c5a2ab914ac3.jpg_100x100xz.jpg';
      fetch(imageUrl)
        .then(response => response.arrayBuffer())
        .then(buffer => {
          const fileType = window.fileType.fromBuffer(new Uint8Array(buffer));
          const image = document.getElementById('image');
          image.addEventListener('load', () => {
            const width = image.naturalWidth;
            const height = image.naturalHeight;
            console.log(`The file type is ${fileType.ext}`);
          });
          image.addEventListener('error', () => {
            console.log('The file is not a valid image');
          });
          const objectUrl = window.URL.createObjectURL(new Blob([buffer], { type: fileType.mime }));
          image.src = objectUrl;
        })
        .catch(error => {
          console.log(`Error fetching image: ${error.message}`);
        });
    </script>
  </body

But it gives error:
GET https://cdn.jsdelivr.net/npm/file-type/dist/index.umd.js net::ERR_ABORTED 404
Error fetching image: Cannot read properties of undefined (reading ‘fromBuffer’)

Can someone please guide me on how to correctly determine the file type of an image in JavaScript? The ideal output for the image in question should be ‘webp’.”

How to switch variable into another variable once the condition is get in java if else if statement

I just want to know how to switch the variable into another variable.. For example, I have a an individual eggs… its 12 will consider as dozen eggs, in program, if I type 12 in individual eggs, how to put a code to switch it in dozen egg..

like this:

if (individualEggs==12)
convert it to dozenEggs:

the output in screen will be like this:
price of dozen egg = 1 usd
price of individual egg = .1 usd

dozenEggs = 1
individualEggs = 12

THE ANSWER MUST BE 2 USD
and not 2.2 usd… when it is computed.. please help me..

cascading dropdown list when changing the first select and second select , the third select box input is not getting append in the first time

<html>
  <head>
    <style type="text/css">
      @font-face {
        font-weight: 400;
        font-style: normal;
        font-family: "Circular-Loom";

        src: url("chrome-extension://liecbddmkiiihnedobmlmillhodjkdmb/fonts/CircularXXWeb-Book.woff2") format("woff2");
      }

      @font-face {
        font-weight: 500;
        font-style: normal;
        font-family: "Circular-Loom";

        src: url("chrome-extension://liecbddmkiiihnedobmlmillhodjkdmb/fonts/CircularXXWeb-Medium.woff2") format("woff2");
      }

      @font-face {
        font-weight: 700;
        font-style: normal;
        font-family: "Circular-Loom";

        src: url("chrome-extension://liecbddmkiiihnedobmlmillhodjkdmb/fonts/CircularXXWeb-Bold.woff2") format("woff2");
      }

      @font-face {
        font-weight: 900;
        font-style: normal;
        font-family: "Circular-Loom";

        src: url("chrome-extension://liecbddmkiiihnedobmlmillhodjkdmb/fonts/CircularXXWeb-Black.woff2") format("woff2");
      }
    </style>
  </head>
  <body>
    <div class="form">
      <div class="box-root padding-top--24 flex-flex flex-direction--column" style="flex-grow: 1; z-index: 9">
        <div class="formbg-outer">
          <div class="formbg">
            <div class="formbg-inner padding-horizontal--48">
              <form class="konnectify-form">
                <div class="field padding-bottom--24">
                  <label for="name">name</label>
                  <input type="text" id="name" name="name" placeholder="" fdprocessedid="wbtyj" />
                </div>
                <div class="field padding-bottom--24">
                  <label for="name_1">name</label>
                  <select name="name_1" id="name_1" fdprocessedid="thnpcl">
                    <option value="" selected="selected" disabled="" hidden="">Select name_1</option>
                  </select>
                </div>
                <div class="field padding-bottom--24">
                  <label for="item2">item2</label>
                  <select name="item2" id="item2" fdprocessedid="jqik">
                    <option value="" selected="selected" disabled="" hidden="">Please select name_1 first first</option>
                  </select>
                </div>
                <div class="field padding-bottom--24">
                  <label for="item3">item3</label>
                  <select name="item3" id="item3" fdprocessedid="zv9s4h">
                    <option value="" selected="selected" disabled="" hidden="">Please select item2 first</option>
                  </select>
                </div>

                <div class="submit-button">
                  <div class="field padding-bottom--25">
                    <input type="submit" name="submit" value="Submit" fdprocessedid="za5o8g" />
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>

    <script>
      const data = {
        fieldId: "2a88206e-9ede-45e1-b739-5603262e7ef8",
        orderId: 1,
        actionMode: "view",
        sampleData: "dependent",
        fieldName: "name_1",
        inputType: "dependent",
        label: "name",
        config: { requires: false, itemsField: "item3", subcategoryField: "item2" },
        options: [
          {
            id: 0,
            label: "tn",
            isRepeated: false,
            subCategory: [
              {
                id: 0,
                label: "mad",
                items: [
                  { id: 0, label: "qq" },
                  { id: 1, label: "ww" },
                ],
              },
              {
                id: 1,
                label: "cbe",
                items: [
                  { id: 0, label: "ss" },
                  { id: 1, label: "pp" },
                ],
              },
              { id: 2, label: "salem", items: [] },
            ],
          },
          {
            id: 1,
            label: "ker",
            isRepeated: false,
            subCategory: [
              {
                id: 0,
                label: "pal",
                items: [
                  { id: 0, label: "aa" },
                  { id: 1, label: "bb" },
                ],
              },
              {
                id: 1,
                label: "gur",
                items: [
                  { id: 0, label: "cc" },
                  { id: 1, label: "dd" },
                ],
              },
            ],
          },
        ],
        isSaved: true,
      };
      console.log(data, "data");
      let res = {};
      for (let subCategories of data.options) {
        let subCategory = {};
        for (let items of subCategories.subCategory) {
          let itemValue = [];
          for (let item of items.items) {
            itemValue.push(item.label);
          }
          //   console.log(subCategory[items.label], "itemValue");
          subCategory[items.label] = itemValue;
          //   console.log(itemValue, "itemvalue");
        }
        // console.log(res[subCategories.label], "res[subCategories.label]");
        res[subCategories.label] = subCategory;
        // console.log(subCategory, "sub");
      }
      const subjectObject = res;
      //   const subjectObject = {
      //     tn: { mad: ["tam", "anna", "pudhu"], cbe: ["gana", "t"], salem: [] },
      //     ker: { pal: ["aa", "bb"], gur: ["qq", "ww"] },
      //   };
      window.onload = function () {
        var parentOption = document.getElementById("name_1");
        var subCategory = document.getElementById("item2");
        var items = document.getElementById("item3");
        for (var x in subjectObject) {
          parentOption.options[parentOption.options.length] = new Option(x, x);
        }
        parentOption.onchange = function () {
          console.log(items, "items.length", items.length);
          console.log(subCategory, "subCategory.length", subCategory.length);
          items.length = 1;
          subCategory.length = 1;
          for (var y in subjectObject[this.value]) {
            subCategory.options[subCategory.options.length] = new Option(y, y);
          }
        };
        subCategory.onchange = function () {
          items.length = 1;
          var z = subjectObject[parentOption.value][this.value];
          for (var i = 0; i < z.length; i++) {
            items.options[items.options.length] = new Option(z[i], z[i]);
          }
        };
      };
      const formEle = document.querySelector(".konnectify-form");
      formEle.addEventListener("submit", (event) => {
        event.preventDefault();
        const formData = new FormData(formEle);
        const data = Object.fromEntries(formData);
      });
    </script>
  </body>
</html>

i had tryed by emptying the select box but its not working
When I change the parent dropdown for the first time, the cascading dropdown is getting populated correctly. When I change the parent dropdown for the second time, the value is not getting populated. The third time, the parent dropdown is changed, the previous value is getting populated.

But in the console, the correct data is getting populated. The only issue is it is not getting bound in the UI.

What could be the issue?

TOP ‘rock, paper, scissors’ help: alert undefined

I was wondering if someone could help me with this small bug
I’m currently working on TOP rock paper scissors project and my code works as it’s suppose to but it alerts ‘undefined’ before the Scoreboard alert and I’m not sure why

When I look at the console it also prints the following message:
Uncaught TypeError: Cannot read properties of null (reading ‘toLowerCase’)
at game (game-2.js:47:43)
at game-2.js:55:1

let playerScore = 0;
let computerScore = 0;


function getComputerChoice() {
    let choices = ['rock', 'paper', 'scissors'];
    return choices[Math.floor(Math.random() * choices.length)];
}

function playRound(playerSelection, computerSelection) {
if ( (playerSelection == "paper" && computerSelection === "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors") ) {
    playerScore += 1;
    alert("You win! " + playerSelection + " beats " + computerSelection);
} 
else if ( (playerSelection == computerSelection) ) {
    alert("It's a tie! You both chose " + playerSelection);
} else {
    computerScore += 1;
    alert("You lost! " + computerSelection + " beats " + playerSelection);
}

}

function roundScore() {
    alert('Scoreboard: Player ' + playerScore + ' x ' + computerScore + ' Computer');
    
}

function checkWinner() {
    if (playerScore > computerScore) {
        return 'You beat the machine! The world isn't doomed after all.';
    }
    else if (playerScore == computerScore) {
        return 'Tie! Humans and robots are equally smart.'
    }
    else{
        return 'The computer wins. The Age of Ultron is coming.';
    }    
}


function game() {
    for (let i = 0; i < 5; i++) {
        let playerSelection = prompt('Choose your player! Rock, paper or scissors?'); 
        playerSelection = playerSelection.toLowerCase(); 
        let computerSelection = getComputerChoice(); 
        alert('The computer chose ' + computerSelection);
        alert(playRound(playerSelection, computerSelection));
        roundScore();
}}


game();
alert( checkWinner() );

If anyone could help me that would be awesome, thank you!

I tried different ways of writing prompt().toLowerCase() but nothing works

TypeError: seed is not a function

I am trying to seed data into a database, but I am getting an error that says:

    TypeError: seed is not a function
    at main (C:Usersmarcesrctravlrapp_servermodelsdb.js:30:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
C:Usersmarcesrctravlrnode_modulesmongooselibconnection.js:972
    throw new MongooseError('Connection.prototype.close() no longer accepts a callback');

I thought I installed seedgoose using my terminal. What could be the problem? Here is part of my code:

db.js

    const mongoose = require('mongoose');
const host = process.env.DB_HOST || '127.0.0.1';
const conn_uri = `mongodb://${host}/travlr`;

const {seed} = require('./seed');

// Register models
require('./trips');

mongoose.connection.on('connected', () => console.log('CONNECTED!'));
mongoose.connection.on('error', err => console.log(err));
mongoose.connection.on('disconnected', () => console.log('DISCONNECTED!'));

mongoose.set('strictQuery', false);

// Kill MongoDB connections before exiting app
const gracefulShutdown = (msg, callback) => {
    mongoose.connection.close( () => {
        console.log(`Mongoose disconnected due to ${msg}`);
        callback();
    });
}
process.once('SIGUSR2', () =>
    gracefulShutdown('nodemon restart', () => process.kill(process.pid, 'SIGUSR2')));
process.on('SIGINT', () =>
    gracefulShutdown('app termination', () => process.exit(0)));

async function main() {
    await mongoose.connect(conn_uri);
    await seed();
}

main().catch(console.log);

Why does passing “this” from html become undefined in typescript?

I have an html button. When I pass “this” in the “onclick” to a typescript function, it says it’s the element is “undefined”.

I have an html table with a delete button in each row. I want to delete the row when it is clicked.

function deleteRow(btn) {
  let row = btn.parentNode.parentNode;  //this should be the <tr> row element
  let name = row.getElementsByTagName("TD")[0].innerHTML;
  let table = document.getElementById("nutrient-table");
  table.deleteRow(row.rowIndex);
};
<tr>
  <td>Vitamin C</td>
  <td class="dv"></td>
  <td class="dv">2%</td>
  <td>
    <button class="fa fa-minus" (click)="deleteRow(this)"></button>
  </td>
</tr>

The error message says

btn.parentNode is undefined

Another place I saw “btn is undefined”

Any ideas?

getElementbyID returning null after DOM loaded

User clicks on CONNECT button to reveal a hidden div and hide the div conatining the calling CONNECT button.

a) If user Clicks Cancel, the annonymous function in the onClick event process everything correctly.

b) If user clicks on the SAVE button in the newly revealed div to hide itself and reveal the original button which returns NULL element. The reason for not putting the saveWlan() call in an annonymous function is that ‘this’ is undefined and I can’t perform additional processing in the saveWlan function and I don’t want 20 copies of the annonymous function in the page.

The below snippet strips out a lot of CSS and javascript code that generates the given HTML and shows the state of the HTML page at the time before the user clicks the first button to show the problem.

The onClick=’saveWlan(this)’ function call provides a valid ‘this’ object who’s IDs, I can query, deconstruct with the split function and rebuild the ID of the element I want to modify (the div container of the CONNECT button).

The console.log() statements show that I have correctly read the this.id property and reconstructed the ID string of the element I wish to modify, but at this point the document.getElementById(…) returns NULL.

The elements can be seen in the developer tools –> elements inspector before the process starts, so the objects are not NULL.

const saveWlan = async(element) => {
  console.log("[savingWlan]");
  document.getElementById('blocker-title').innerHTML = 'Saving';
  document.getElementById('blocker').classList.remove('hidden'); // this works !!


  console.log("element.id = " + element.id);

  const nameParts = element.id.split('-'); // "scanWlan-3-save"
  const wlanPrefix = nameParts[0]; // returns scanWlan
  const wlanIndex = nameParts[1]; // returns 3
  // I can now add a suffix of my choosing

  var idStringEdit = `'${wlanPrefix}-${wlanIndex}-edit'`; //scanWlan-3-edit
  var idStringConnect = `'${wlanPrefix}-${wlanIndex}-connect'`; //scanWlan-3-connect

  console.log("idStringEdit = " + idStringEdit); //idStringEdit = 'scanWlan-3-edit'
  console.log("idStringConnect = " + idStringConnect); //idStringConnect = 'scanWlan-3-connect'


  document.getElementById(`'${wlanPrefix}-${wlanIndex}-connect'`).classList.add('hidden'); // this doesn't work
  document.getElementById(`'${wlanPrefix}-${wlanIndex}-edit'`).classList.remove('hidden');

  const ssid = wlan.ssid;
  const password = document.getElementById(`'${wlanPrefix}-${wlanIndex}-password'`).value;

  console.log("wlanIndex = " + wlanIndex);
  console.log("wlanIndex.ssid = " + ssid);
  console.log("wlanIndex.password = " + password);

  var data = new FormData();
  data.append("ssid", ssid);
  data.append("password", password);

  // transfer data to server
}


// Window Interval functions:
//Interval never stops, we just stop responding to it!!

var scanning = true; //global scope 

function startScan() { // resume scanning for WiFi Networks every 15 seconds...  
  if (scanning == false) { // but only once every 15 seconds... ;)
    scanning = true;
  }
}

function doWifiScan() {
  if (scanning == true) fetchScannedList();
}

function stopScan() {
  scanning = false;
}
#blocker {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  top: 0;
  left: 0;
  z-index: 1;
  position: fixed;
  height: 100%;
  width: 100%;
  background: steelblue;
  opacity: 0.95;
  color: white;
}

.spacer {
  display: block;
  width: 100%;
  height: 80px;
}

.small {
  height: 5px;
}

.hidden {
  display: none !important;
}

.btn-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.btn-container input {
  width: 15%;
  border: 1px solid lightgrey;
  border-radius: 5px;
  padding: 15px 32px;
  margin: 10px 5px;
}

.btn {
  border-radius: 5px;
  padding: 15px 10px;
  text-align: center;
  transition-duration: 0.4s;
  margin: 10px 5px;
  cursor: pointer;
  text-decoration: none;
}

.edit {
  background-color: dodgerblue;
  border: solid 1px dodgerblue;
  color: white;
}

.edit:hover {
  color: dodgerblue;
}

.save {
  background-color: yellowgreen;
  border: solid 1px yellowgreen;
  color: white;
}

.save:hover {
  color: yellowgreen;
}

.cancel {
  background-color: black;
  border: solid 1px black;
  color: white;
}

.cancel:hover {
  color: black;
}

.btn:hover {
  background-color: white;
}
<body>
  <div>
    <div class="row" id="rowScanWlan-3">
      <!-- row -->
      <div class="wifi-info">
        <!-- column 60%-->
        <div class="wifi-name" id="scanWlan-3-ssid">
          GI-VoIP
        </div>
      </div>
      <div class="column">
        <div class="btn-container" id="scanWlan-3-connect">
          <img src="" alt="||" height="35px" border="0">
          <img src="" alt="C" height="35px" border="0">
          <a id="scanWlan-3-show" class="btn edit" onclick="(function(){
      document.getElementById('scanWlan-3-edit').classList.remove('hidden');
      document.getElementById('scanWlan-3-connect').classList.add('hidden');
          stopScan();
      })();">
        Connect
    </a>
        </div>
      </div>
    </div>

  </div>
  <div class="spacer small">
    <!--  forces the row above to FILL a row, rather than wrapping multiple networks onto a single row -->
  </div>
  <div class="more-info hidden" id="scanWlan-3-edit">
    <div class="wifi-info">Enter Password:
      <input type="text" name="password" id="scanWlan-3-password"></div>
    <div class="column">
      <div class="btn-container" id="rowSsidScanWlan-3">
        <a id="scanWlan-3-save" class="btn save" onclick="saveWlan(this)">
         Save
        </a>
        <a id="scanWlan-3-cancel" class="btn edit" onclick="(function(){
      document.getElementById('scanWlan-3-password').value='';
      document.getElementById('scanWlan-3-edit').classList.add('hidden');
      document.getElementById('scanWlan-3-connect').classList.remove('hidden');
          startScan();
      })();">
      Cancel
        </a>
      </div>
    </div>
  </div>
  </div>

  <div id="blocker" class="hidden">
    <h2 id="blocker-title">Loading</h2>
    <div id="Saved">
      <p id="finished">
      </p>
    </div>
  </div>
</body>

Formik Initial Values don’t populate from api call

I’m fetching data from my backend based on an id in the params. The data is fetched fine from looking at the console log including different values, such as product.title.

I want to be able to edit my product using formik but for some reason, the initial values won’t populate in the input field even tho everything is set perfectly.

When i console log formik.initialvalues, it shows that product.title is undefined.

Here is my useEffect code to fetch data:

    useEffect(() => {
        const fetchProductById = async () => {
          try {
            const response  = await axios.get(`http://localhost:3005/product/${prodId}`)
            setProduct(response.data)
          } catch (err) {
            console.error(err)
          }
        }
        fetchProductById();
      },[prodId]);

Here is my formik initial values (I’m using product.title as an example):

    const editProductFormik = useFormik({
      initialValues: {
        title: product.title,
        description: '',
        price: '',
        images: images,
        coverImage: coverImage,
        location: '',
        age: '',
        condition: '',
        Category: '',
        SubCategory : '',
        seller : seller,
      },

Here is my input for Product Title :

                <Box>
                <Input mr={2} width="250px" type="title" value={editProductFormik.values.title} onChange={editProductFormik.handleChange("title")} marginBottom="10px" />
                {editProductFormik.touched.title && editProductFormik.errors.title ? (
                    <Text mt={2} color="red" fontWeight="bold">{editProductFormik.errors.title}</Text>
                  ) : null}
                </Box>

Validating zip file using adm-zip and cypress throws out of memory error

I was trying to validate zip file contents using adm-zip and cypress, but cypress throws out of memory error. The Zip file could contain .txt or .pdf or .ppt or .docx files. I would like to validate below in the zip file:

   a) no of files
   b) type of files 

“cypress”: “v12.8.1”,

“adm-zip”: “^0.5.10”,

“path”: “^0.12.7”

Note: Using Adm-zip from this https://www.npmjs.com/package/adm-zip

test.spec.js

  import HelperFunctions from "../../../support/helperFunctions";
  const helperFunctions = new HelperFunctions();
 
 
  it('Download test', function () {
    cy.get('button[type="submit"]').contains("Download").click({force:true});
    helperFunctions.validateZip();
   })

helperFunctions.js

validateZip () {
    const downloadsFolder = Cypress.config('downloadsFolder')
    const downloadedFilename = path.join(downloadsFolder, 'ComprehensionStrategyTeachingResourcePackSummarisingZipFile.zip')
  
    // wait for the file to be fully downloaded by reading it (as binary)
    // and checking its length
    cy.readFile(downloadedFilename, 'binary', { timeout: 35000 }).should('have.length.gt', 300)
  
    // unzipping and validating a zip file requires the direct access to the file system
    // thus it is easier to perform the checks from the `setupNodeEvents` function in the Cypress configuration that runs in Node
    // see the "on('task')" code in the `setupNodeEvents` function to see how we can read and validate a Zip file
    cy.task('validateZipFile', downloadedFilename)
  }

cypress.config.js

const AdmZip = require("adm-zip");

const { defineConfig } = require('cypress')

module.exports = defineConfig({
   e2e: {
    setupNodeEvents(on, config) {
      .....


      on("task", {
        validateZipFile: (filename) => {
          return validateZipFile(filename);
        }, 
      });

      return config;
    },
    baseUrl: "https://staging-qa.someurl.com/",
    specPattern: "cypress/e2e/**/*.spec.{js,jsx,ts,tsx}",
  },
})
    
    function validateZipFile (filename) {
      // now let's validate the downloaded ZIP file
      // Tip: use https://github.com/cthackers/adm-zip to load and unzip the Zip contents
      console.log('loading zip', filename)
    
      const zip = new AdmZip(filename)
      const zipEntries = zip.getEntries()
      const names = zipEntries.map((entry) => entry.entryName).sort()
    
      console.log('zip file %s has entries %o', filename, names)
      // any other validations?
      return null
    }

plugin:vite:vue Error parsing JavaScript expression: Unexpected token (1:145)

I’m writing a progress step in vue. However I’m not able to run it due to the error and I cannot figure out the token in question. The error occurs on the 4th line below (inside class). Any idea how to fix this?

<div class="steps-container" :style="cssStyle">
    <ul class="steps-list">
        <li class="step" v-for="(step, index) in data.steps" :key="index" 
        :class="(index == data.currentStep) ? step-active : '', (index < currentStep) ? 'step-done' : '', (index == 0 && index == data.currentStep) ? step-done-in-advance : ''">
            <div class="step-bubble">
                <div class="step-count">
                    {{ index + 1 }}
                </div>
                <div class="step-label">
                    {{ step }}
                </div>
                <div class="step-line">
                    <div class="line-fill">
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>

How can I get this Play/Pause Button to work successfully in Safari?

I’ve got this little push-button situation that works in Chrome but not on Safari. I’ve tried experimenting with autoplay in various parts of this but wasn’t lucky. What am I missing here? Please note, I didn’t have a sound posted online to share here. I was originally using a Base64 encoded sound file. I will edit this if I’m able to find a shorter one, but the editor here in SO was limiting my post with that included.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Audio Play and Pause</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <audio id="audioElement" preload="auto">
    <source src="sound.mp3" type="audio/mpeg">
  </audio>
  <button id="playPauseButton" onmousedown="playAudio()" onmouseup="pauseAudio()" ontouchstart="playAudio()" ontouchend="pauseAudio()">Play/Pause</button>

  <script>
    function playAudio() {
      document.getElementById("audioElement").play();
    }

    function pauseAudio() {
      document.getElementById("audioElement").pause();
    }
  </script>
</body>

</html>

Here’s a link to a JS Fiddle, if it helps. TIA.

https://jsfiddle.net/anonymouspenguin/kevhq7x1/1/