We help IT Professionals succeed at work.

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

894359
894359 asked
on
213 Views
Last Modified: 2018-10-27
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

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions