Link to home
Create AccountLog in
Avatar of MJ
MJFlag for United States of America

asked on

Capture Duplicate Class Name Text and Clean Up Currency, if Present?

How to get multiple values from N # of same class name? In the example below I want to grab the text whenever there is a class name of  "offer-product-label" and divide with a pipe. If there is a currency amount, strip to only dollar amount. So the below example would return => "Line of Credit|450000"

I started with:
var pResult;
		 
	    var prodResult = document.getElementsByClassName("offer-product-label");
        for (var i = 0; i < prodResult.length; i++) {
            pResult = prodResult[i].innerText;
        }

Open in new window

for the below:
<fieldset class="fieldset-line">
                            <legend tabindex="0">Request summary</legend>
                            <dl tabindex="0">
                                <dt>Product</dt>
                                <dd class="offer-product-label">Line of Credit</dd>
                            </dl>
                            <dl tabindex="0">
                                <dt>Amount Requested</dt>
                                <dd class="offer-product-label">$45,000.00</dd>
                            </dl>

                        </fieldset>

Open in new window



Thanks!
Avatar of leakim971
leakim971
Flag of Guadeloupe image

test page : https://jsfiddle.net/6hv7tond/

<fieldset class="fieldset-line">
    <legend tabindex="0">Request summary</legend>
    <dl tabindex="0">
        <dt>Product</dt>
        <dd class="offer-product-label">Line of Credit</dd>
    </dl>
    <dl tabindex="0">
        <dt>Amount Requested</dt>
        <dd class="offer-product-label">$45,000.00</dd>
    </dl>
</fieldset>
<script>
    var pResult = []; // WE START WITH AN ARRAY
    var prodResult = document.getElementsByClassName("offer-product-label");
    for (var i = 0; i < prodResult.length; i++) {
        pResult.push(prodResult[i].innerText); // WE ADD EVERY TEXT WE FIND
    }
    pResult = pResult.join("|"); // WE TRANSFORM OUR ARRAY IN A STRING AND WE SEPARATE EACH STRING BY A PIPE
    // CHECKING :
    alert(pResult);
</script>

Open in new window

Avatar of MJ

ASKER

Hi Again Leakim,
Thanks again for all your help! What about the part of cleaning up the currency?
replace :
pResult.push(prodResult[i].innerText.replace)

Open in new window

by :
var str = prodResult[ i ].innerText;
pResult.push( /(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$/.test(str) ? Number(str.replace(/[^0-9.-]+/g,"")) : str );

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of MJ

ASKER

I'm forever indebted to you! I will give you my 1st born! :0)