SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find and replace all prices in html

    Can anyone help me with the js to find and replace any prices within the innerHTML?

    Matching eg:

    25.00
    $15
    1000.45
    1000,999

    i wan't to find all instances of these and replace them with alternative strings

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Skyline,

    Here is a simple example of javascript using innerHTML

    <html>
    <head>

    <script type="text/javascript">
    function change(){
    document.getElementById("header").innerHTML="New Header";
    }
    </script>

    </head>
    <body>

    <h1 id="header">Old Header</h1>
    <a href="#" onclick="change()">click me</a>
    <p>"Old Header" was changed to "New Header"</p>

    </body>
    </html>
    This should show how you can change the value if you can access the element. In your case is there a way you can get prices. Does this help?

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    Thing is i'm looking for the way to find all of the prices on the page so they can then be changed. A regex of some description i guess?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably. I dont know. But here is a simple sample that changes the prices if you know the value atleast.

    <html>
    <head>
    <script type="text/javascript">
    function searchPage(){
    var bodyText = document.body.innerHTML;
    document.write(bodyText.replace("$15","$300"));
    }
    </script>
    </head>
    <body>
    <button onclick="searchPage()">Click me</button>
    <br/>
    €25.00
    <br/>
    $15
    <br/>
    &#163;1000.45
    <br/>
    &#165;1000,999
    </body>

    </html>

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmm unfortunately the prices are unknown! Anyone good with js regex?

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by skyline View Post
    Mmm unfortunately the prices are unknown! Anyone good with js regex?
    What are the currency formats you expect to be dealt with?

    Is it only euro, dollar, pound and yen, in the formats that you provided, that you want to match?

    Code:
    25.00
    $15
    1000.45
    1000,999
    I'm no expert on regular expressions, but I think you might be after something like this:

    Code javascript:
    /[\$](\d+(\,\d+)*|(\d+))(\.\d+)?/
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that seems to find them!
    Now how would i go about replacing each of the occurences with an ajax response?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    By using http://api.jquery.com/jQuery.get/ or if you want to DIY, investigate Bulletprof Ajax
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •