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 7790095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:29:07+00:00 2026-06-01T21:29:07+00:00

From an open code I have this line var average = parseFloat($(this).attr(‘id’).split(‘_’)[0]), It gets

  • 0

From an open code I have this line

var average = parseFloat($(this).attr('id').split('_')[0]),

It gets the first part of a div id with ‘_’ as delimiter. The problem is that an id cannot start with a number (naming violation convention). So I am going to add a letter before the id value in my php script. How do I insert substr(1) to this var to remove this letter and get ‘average’ as expected?

  • 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-06-01T21:29:09+00:00Added an answer on June 1, 2026 at 9:29 pm

    Assuming you’re talking about this format for an id:

    <div id="A1000_bc"></div>
    

    You can insert the substr(1) like this:

    var average = parseFloat(this.id.split('_')[0].substr(1));
    

    I might prefer to do it like this so it’s a little less presumptious about the exact format and just grabs the first floating point numeric sequence:

    var average = parseFloat(this.id.match(/[\d\.\+\-]+/)[0]);
    

    Also, notice how I removed the jQuery. $(this).attr("id") performs a lot worse than this.id and offers no advantages here. jQuery should be used only when it’s actually better than plain JS.

    Both of these methods assume you are only going to present the code with properly formatted ids. If you want to handle a default condition when the id is not in the right format, then you will need multiple lines of code with some if conditions to check for validity and offer a default result when not valid.

    Both options work here: http://jsfiddle.net/jfriend00/B4Rga/

    Incidentally, if you control the HTML here, then there are better places to put data like this than in an id. I’d suggest a custom data attribute (HTML5 standard, but works everywhere).

    <div id="whatever" data-avg="3.5"></div>
    

    Then, you can get the data like this without having to parse it:

    var average = parseFloat(this.getAttribute("data-avg"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this line of code: $('#sitesAccordion .groupOfSites').click(function() { var lastOpenSite = $(this).siblings().hasClass(':not(.closedTab)'); console.log(lastOpenSite);
I have used this code var dest1 = File.AppendText(Path.Combine(_logFolderPath, log1.txt)); dest1.WriteLine(line.Trim()); to write to
I have built an open-source application from the source code. Unfortunately, the original executable
I have this code: <script type=text/javascript> var url = http://www.xxxxx.xxx/xxxxxxxxx; var txt; var id1;
I have this code: function render_message(id) { var xmlHttp; xmlHttp=new XMLHttpRequest(); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4)
Basically I am using some open source code called OrderedDictionary that is derived from
I am using the following code to open a new Activity from my current
I need to open a new window from code-behind on post-back if a specific
When I read source from open source projects I often find a comment line
I am getting this error on code that used to work. I have not

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.