getButterfly
Resume

NAVIGATION

ATTRIBUTION

getButterfly
Dublin, Ireland

All Content Copyright ©2019
getButterfly

How to extract a value from an element and, optionally, round it

This JavaScript snippet will extract a price from a DOM element, remove all alphanumeric characters, such as currency or symbols or other words, and return it.

The second snippet will also round the value.

let price,
    priceValue;

// Extract value
price = document.getElementById('price');
priceValue = parseInt(price.innerText.replace(/[^0-9.]/g, ''));

document.getElementById('result').innerHTML = priceValue;

// Extract value and round it
price = document.getElementById('price');
priceRounded = parseFloat(price.innerText.replace(/[^0-9.]/g, '')).toFixed(2);

document.getElementById('result-rounded').innerText = priceRounded;

See a fiddle here.

Find more JavaScript tutorials, code snippets and samples here or more jQuery tutorials, code snippets and samples here.

Added by Ciprian on Thursday, August 22, 2019 in JavaScript

Do you want better SEO? More traffic? More conversions? More growth? We help companies exponentially grow their traffic and conversions, while outranking their competitors. With more than 10 years of experience, we’ve learned what is valuable to our clients.
SEO Dublin | SEO Malta

Related Articles

My Battle Tested Recommendations
Disclaimer: These recommendations contain affiliate links.

Jetpack
Dreamhost
WordPress.com
CodeCanyon
SEMrush
SEMrush

Privacy Policy