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!
JScript

Avatar of undefined
Last Comment
MJ
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
MJ
Flag of United States of America image

ASKER

Hi Again Leakim,
Thanks again for all your help! What about the part of cleaning up the currency?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MJ
MJ
Flag of United States of America image

ASKER

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

JScript is Microsoft's dialect of the ECMAScript standard that is used in Microsoft's Internet Explorer. JScript is implemented as an Active Scripting engine, so it can be "plugged in" to OLE Automation applications that support Active Scripting, such as Internet Explorer, Active Server Pages, and Windows Script Host. It also means such applications can use multiple Active Scripting languages, e.g., JScript, VBScript or PerlScript.

7K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo