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!
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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

894359Author Commented:
Hi Again Leakim,
Thanks again for all your help! What about the part of cleaning up the currency?
leakim971PluritechnicianCommented:
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

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

<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++) {
			var str = prodResult[ i ].innerText;
      if(/(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$/.test(str))
        pResult.push( Number(str.replace(/[^0-9.-]+/g,"")) );
      else
        pResult.push(str);
      
    }
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
894359Author Commented:
I'm forever indebted to you! I will give you my 1st born! :0)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.