Find price in string

<div class="product-price"><span class="price"><strike>349,00&nbsp;</strike></span>49,00&nbsp;<span class="currency">DKK</span></div>

How does I find 49,00 ?
I can´t change the DOM, so I need to find it due jQuery.
Jesper ChristensenProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Flabio GatesCommented:
$(document).ready(function(){
  alert($('.product-price').contents().eq(1).text());
});

Open in new window


from https://api.jquery.com/contents/
0

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
Julian HansenCommented:
var price = $('.product-price span.price')[0].nextSibling.nodeValue.trim();

Open in new window


What the above is doing.
1. find the span.price element inside the element with class product-price.
2. [0] means return the first JavaScript node found (this is now a JavaScript node not a jQuery object)
3. This means we can now use the JavaScript functions on the node. nextSibling will find the text node immediately after the price <span>
4. .nodeValue returns the value of the string
5. Finally we trim the value to get rid of trailing and following white space characters.
0
Jesper ChristensenProgrammerAuthor Commented:
Both solutions works :)
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Julian HansenCommented:
When I test the accepted solution I get 349 - where you want 49 - not sure how that equates to a working solution?

For the accepted solution to work on your data you would need to do this

$('.product-price').contents().eq(2).text()

Open in new window

0
Flabio GatesCommented:
When I test the accepted solution I get 349
@Julian, that's odd.
This fiddle shows I get 49,00.
0
Julian HansenCommented:
<div class="product-price">
   <span class="price">
      <strike>349,00&nbsp;</strike>
    </span>
    49,00&nbsp;
   <span class="currency">DKK</span>
</div>

Open in new window

When you dump this with
console.log($('.product-price').contents());

Open in new window

Gives the following
{…}
0: #text "
   "
1: <span class="price">
2: #text "
    49,00 
   "
3: <span class="currency">
4: #text "
"
context: HTMLDocument http://mrcp9/ee/t2981.html
length: 5
prevObject: Object { 0: div.product-price, length: 1, selector: ".product-price", … }
__proto__: Object { jquery: "1.11.1", constructor: jQuery(), length: 0, … }
t2981.html:54:2

Open in new window

There is an empty text node linked to the original <div> that increases the index of the target node which is the third (index 2) element.

Here is a link to working sample. Use F12 to view the results
0
Flabio GatesCommented:
aha. that's because I used the HTML as provided by the OP. yours has indentation and line-breaks hence the additional text node.
0
Julian HansenCommented:
Ok but you cannot rely on a solution that requires the formatting of the html to be laid out in a certain way. One space in that line i.e

<div class="product-price"> <span class="price"><strike>349,00&nbsp;</strike></span>49,00&nbsp;<span class="currency">DKK</span></div>

Open in new window

And the solution breaks.
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
JavaScript

From novice to tech pro — start learning today.