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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:30:28+00:00 2026-05-16T21:30:28+00:00

I have a javascript function that changes a user preference, if they want weights

  • 0

I have a javascript function that changes a user preference, if they want weights to be metric or in imperial.

On my page I print out the weight, IE:

This is some description:

This product weights <b>200 KG</b>

Blah blah

I need to make it so that Javascript changes the weight measurement for all weights on the page, and am not sure the best way to tackle this. Can you create your own tags, or tag properties?

Thanks for any suggestions, JQuery is fine.

  • 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-16T21:30:28+00:00Added an answer on May 16, 2026 at 9:30 pm

    I’d recommend using strong rather than b, but the below will work for either of them.

    Edit: Better solution than using a class, with working example, below.

    Tag the weights with a class, e.g.:

    This product weighs <strong class='weight'>200 KG</strong>
    

    Then in your JavaScript, you can switch like so:

    $('.weight').each(function() {
        var $this = $(this);
        var original = $this.text();
        var converted = /* ...convert the weight here... */;
        $this.text(converted);
    });
    

    Obviously you can condense that a bit, kept it verbose for clarity.

    Better solution:

    Tag the elements with the original weight (the one you store in your database) as a data-weight attribute, e.g.:

    This product weighs <strong data-weight='200'>200 KG</strong>
    

    Then convert from that value:

    $('[data-weight]').each(function() {
        var $this = $(this);
        var value = $this.attr("data-weight");
        if (usingMetric) {
            $this.text(value + " KG");
        }
        else {
            value = parseFloat(value) * 2.20462262; // Convert to imperial
            $this.text(value.toFixed(2) + " lbs");
        }
    });
    

    Working example: http://jsbin.com/icure3

    Attributes in the form data-xyz will pass validation as of HTML5; prior to HTML5 they are technically invalid, but harmless. But if you prefer a version that doesn’t use data-weight, you can do it with classes instead: http://jsbin.com/icure3/2 (inspired by Reigel’s answer to Tom’s other question).

    If you see a delay when switching between metric and imperial on slower browsers (I’m looking at you, Microsoft), you can help jQuery’s selector engine optimize by giving it more context than just “[data-weight]”. jQuery’s engine supports nearly all of CSS3. For instance, if you always use the data-weight attribute only one one kind of tag (say, strong tags), change the selector to “strong[data-weight]” so the selector engine knows it can optimize for just one specific tag name. Similarly, if all of these are inside some container (e.g., a div with the ID “productList” for instance), you can help the engine out even more (“#productList strong[data-weight]”) so it can ignore anything outside that div. I’ve kept it completely general above. But probably only bother if the page is big and complex enough that you see a performance issue.

    Final thought: To throw a bone to browsers with JavaScript disabled, you might include both values in a title attribute as well, e.g.:

    This product weighs <strong title='200 KG / 441 lbs' data-weight='200'>200 KG</strong>
    

    …so it shows up as a tooltip on browsers that do that. I included that in the example above.

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

Sidebar

Related Questions

I have a page with a javascript function that changes which radio buttons are
I have a javascript function that changes the innerHtml of a <div> tag to
I have a function in javascript that changes a span class when I click
I have this Javascript function that sends username and password to php file through
I have a JavaScript function that should only run on specific pages on a
I have a JavaScript function that looks like this: function recalculateWhatIfCurrentRate(aEle, aChangedLabel, recordID, yearEndCurrentRate)
I have a javascript function that calls a generic function to make an ajax
I have a javascript function that accepts a number and performs a mathematical operation
I have a javascript function that checks for a date range. Is there any
I have a javascript function that is being built to animate the collapse of

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.