How can I decrease HTML field values with jQuery?

Asked

Viewed 164 times

3

I have a class in PHP who captures the URL from an external site, this site itself returns me a HTML on the model:

<div id="isOffered">      
    <a class="price  " href="javascript:;">
        <span class="priceText wide  UK">33/20</span>
        <span class="priceText wide  EU">2.65</span>
        <span class="priceText wide  US">+165</span>
        <span class="priceText wide  CH">2.65</span>
        <span class="priceChangeArrow" ></span>
        <input type="hidden" class="betCode" value="0]SK@95553201@362904182@NB*33~20*0*-1*0*0"/>
        <input type="hidden" class="decValue" value="2.65"/>
        <input type="hidden" class="originalBetCode" value="0]SK@95553201@362904182@NB*33~20*0*-1*0*0"/>
     </a>
   </div></div>
                  <div id="s_362904184" class="odds draw suspended">   <div id="isNotOffered" class="hide">    
    <span class="price priceReadonly"></span>
   </div>

   <div id="isOffered">   
    <a class="price  " href="javascript:;">
        <span class="priceText wide  UK">19/10</span>
        <span class="priceText wide  EU">2.90</span>
        <span class="priceText wide  US">+190</span>
        <span class="priceText wide  CH">2.90</span>
        <span class="priceChangeArrow" ></span>
        <input type="hidden" class="betCode" value="0]SK@95553201@362904184@NB*19~10*0*-1*0*0"/>
        <input type="hidden" class="decValue" value="2.90"/>
        <input type="hidden" class="originalBetCode" value="0]SK@95553201@362904184@NB*19~10*0*-1*0*0"/>
     </a>
   </div></div>
                  <div id="s_362904183" class="odds away suspended">   <div id="isNotOffered" class="hide">    
    <span class="price priceReadonly"></span>
   </div>

With the PHP, making use of the DOMDocument() and of DomXpath(), i search the values of tags <span>, decrease the values in 20% of the original, get the value of the field input betCode, and change the values between '~', because they are the divisors responsible for the values in javascript, in PHP a friend here from the forum, a while ago helped me to do in PHP.

But I started to study jQuery, and I saw that I can capture the URL of the site with it, without the need of the PHP, so I did something simple, I took the original website address and replaced it with URL from my server where values are being lowered, I did so:

$('input[value="/services/CouponTemplate.mvc/GetCoupon"]').attr('value', function(_, href){
    return "https://enderecodomeuserver.com/parser.php?url=" + href;
}); 

and it works perfectly, but my doubt is: could I do the same process of decreasing values using the jQuery? As if before the return, I made the decreases using the client’s browser without having to load the server. The jQuery has something similar to DOMdocument?

  • 1

    http://www.w3schools.com/js/js_htmldom.asp, "Javascript can change all the HTML Elements in the page", javascript do what you want with your page.

1 answer

1


For you to get the values using jQuery the syntax looks like this:

var valor1 = $("#seletorUsandoIdDoSpan").text();
var valor2 = $(".seletorUsandoClasseDoSpan").text();
// caso o valor esteja em um input voce deve usar .val() ao inves de .text();

var soma = parseFloat(valor1) + parseFloat(valor2);

OBS:

You’ll have to start differentiating your tags with a unique ID (enumerated for example: priceUK1, priceUK2) or else classes that are inside a container with a unique ID.

Otherwise it will be difficult for you with jQuery to find the value you want because it uses selectors, and somehow this selector has to search for only one element.

Example:

    <div id="isOffered1">      
        <a class="price  " href="javascript:;">
            <span class="priceText wide  UK">33/20</span>
            <span class="priceText wide  EU">2.65</span>
            <span class="priceText wide  US">+165</span>
            <span class="priceText wide  CH">2.65</span>
            <span class="priceChangeArrow" ></span>
            <input type="hidden" class="betCode" value="0]SK@95553201@362904182@NB*33~20*0*-1*0*0"/>
            <input type="hidden" class="decValue" value="2.65"/>
            <input type="hidden" class="originalBetCode" value="0]SK@95553201@362904182@NB*33~20*0*-1*0*0"/>
         </a>
       </div>
    </div>
    <div id="s_362904184" class="odds draw suspended">   <div id="isNotOffered" class="hide">    
        <span class="price priceReadonly"></span>
    </div>

    <div id="isOffered2">   
        <a class="price  " href="javascript:;">
            <span class="priceText wide  UK">19/10</span>
            <span class="priceText wide  EU">2.90</span>
            <span class="priceText wide  US">+190</span>
            <span class="priceText wide  CH">2.90</span>
            <span class="priceChangeArrow" ></span>
            <input type="hidden" class="betCode" value="0]SK@95553201@362904184@NB*19~10*0*-1*0*0"/>
            <input type="hidden" class="decValue" value="2.90"/>
            <input type="hidden" class="originalBetCode" value="0]SK@95553201@362904184@NB*19~10*0*-1*0*0"/>
         </a>
       </div>
    </div>
    <div id="s_362904183" class="odds away suspended">   <div id="isNotOffered" class="hide">    
        <span class="price priceReadonly"></span>
    </div>

With two different id containers: isOffered1 and isOffered2 you can already differentiate your values using jQuery. Example:

var valor1 = $("#isOffered1 .priceText.EU").text(); // output: "2.65"
var valor2 = $("#isOffered2 .priceText.EU").text(); // output: "2.90"
var soma = parseFloat(valor1) + parseFloat(valor2); // output: "5.55"
  • I would have to somehow alter the id's of divs with the PHP, only then to make the sum work?

  • It is not necessary. You always have to pick up any page value via jQuery. But it would make it a lot easier to set the correct selector within $() and find the value you want.

  • If your html is static and only changes Voce values, you don’t even need ID. But if the amount of fields varies it is sometimes impossible to do without.

  • It is dynamic, automatically generates things.

  • In this case it would be good to put a number on each "isoffered" through php. You have this control?

  • Yeah, I’ll put it on later and show you if it’s working. Thanks!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.