简体   繁体   中英

how to make text value to number from the span

I want to make the text value to the number so I can put some calculations in the script.

<p>
     <span class="shopdetailInfoName">CONSUMER</span>
     <span class="shopdetailInfoCont">
         <strike><!--/number/price_consumer/-->
             <span class="won">$</span>
         </strike>
     </span>
</p>

so as you can see the price value coming from this <!--/number/price_consummer/--> which website developer make it is work.

I try to make some script like this ->

var a = document.getElementById ('shopdetailInfoCont');

to bring the price value as a number but .shopdetailInfoCont include "," and "$" so it bring that value to text not the number.

How can I bring the value as the number?

as a result, I want to make like this

(ex ) - product price : 45.50 $ )

if you apply -20$ discount, price will be 25.50$

any help will be so appreciated thanks!

var a = document.getElementById ('shopdetailInfoCont');    
floated_amount = parseFloat(a.replace(/[^0-9.-]/g, ''));

just try this regex to remove strings from your element value.

Thanks.

Firstly, the below shouldn't work cause your class is named shopdetailInfoCont not its id.

var a = document.getElementById ('shopdetailInfoCont'); 

Second, I don't think there is a way to get a value like you wanted, but you what you can do is get 45.50 $ and with javascript parseFloat() function will return 45.50 from that text.

You're using the wrong function to retrieve your element. You have the class set but not an id.

You can use `document.getElementsByClassName() to get your element. This returns an array, so you'll access your element with the 0 index.

document.getElementsByClassName("won")[0].innerHTML = "Product Price: $" + price

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM