Avatar of Jesper Christensen
Jesper Christensen

asked on 

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

Avatar of undefined
Last Comment
Julian Hansen
ASKER CERTIFIED SOLUTION
Avatar of Flabio Gates
Flabio Gates

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
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa 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.
Avatar of Jesper Christensen
Jesper Christensen

ASKER

Both solutions works :)
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

Avatar of Flabio Gates
Flabio Gates

When I test the accepted solution I get 349
@Julian, that's odd.
This fiddle shows I get 49,00.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

<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
Avatar of Flabio Gates
Flabio Gates

aha. that's because I used the HTML as provided by the OP. yours has indentation and line-breaks hence the additional text node.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
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