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

894359
894359 used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

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

Author

Commented:
Hi Again Leakim,
Thanks again for all your help! What about the part of cleaning up the currency?
leakim971Multitechnician
Top Expert 2014

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

Multitechnician
Top Expert 2014
Commented:
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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial