Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6071951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:06:21+00:00 2026-05-23T10:06:21+00:00

I am trying to reduce the repetition in my code but not having any

  • 0

I am trying to reduce the repetition in my code but not having any luck. I reduced the code down to its simplest functionality to try and get it to work.

The idea is to take the last two letters of an id name, as those letters are the same as a previously declared variable and use it to refer to the old variable.

I used the alert to test whether I was getting the right output and the alert window pops up saying “E1”. So I am not really sure why it wont work when I try and use it.

E1 = new Audio('audio/E1.ogg');

$('#noteE1').click(function() {
    var fileName = this.id.slice(4);
    //alert(fileName); used to test output
    fileName.play();
    $('#note' + fileName).addClass('active');
});

The code block works when I use the original variable E1 instead of fileName. I want to use fileName because I am hoping to have this function work for multiple elements on click, instead of having it repeated for each element.

How can I make this work? What am I missing?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:06:22+00:00Added an answer on May 23, 2026 at 10:06 am

    fileName is still a string. JavaScript does not know that you want to use the variable with the same name. You are calling the play() method on a string, which of course does not exist (hence you get an error).

    Suggestion:

    Store your objects in a table:

    var files = {
        E1: new Audio('audio/E1.ogg')
    };
    
    $('#noteE1').click(function() {
        var fileName = this.id.slice(4);
        //alert(fileName); used to test output
        files[fileName].play();
        $('#note' + fileName).addClass('active');
    });
    

    Another suggestion:

    Instead of using the ID to hold information about the file, consider using HTML5 data attributes:

    <div id="#note" data-filename="E1">Something</div>
    

    Then you can get the name with:

    var filename = $('#note').data('filename');
    

    This makes your code more flexible. You are not dependent on giving the elements an ID in a specific format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to reduce repetition and streamline testing/debugging, I'm trying to find the best
I am trying to reduce some code here. I will explain how I have
I'm trying to write a method to reduce the size of any image 50%
I'm trying to reduce the code duplication that exists throughout my asp.net web forms.
I am trying to reduce the height of jquery ui tabs but the formatting
After having a lot of trouble trying to reduce nested ifs (and a script
I'm trying to reduce some repetitive GSP code in my Grails app. The following
I'm trying to reduce the following piece of code: public interface IQuestion<TSite, TStyling, TRelation>
I'm trying to reduce the number of instructions and constant memory reads for a
I'm trying to reduce as much as I can my VB.Net assembly side, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.