11540 sujets

JavaScript, DOM et API Web HTML5

Bonjour,

Je viens de réaliser un script Jquery qui marche très bien mais qui, j'en suis certain, peut-être largement simplifié.
Dans le but de m'améliorer, j'aimerai vos avis (ci-dessous le js + le html) :

J'ai un produit qui a 2 tailles que l'on sélectionne via par bouton radio. Si le stock de l'une des taille est inférieur à 5, on affiche le message suivant : "Il ne reste plus que <strong>1</strong> produit en stock !"



    var stockStr = $('#productWrapper input:checked').parent('.quantityChoice').find('.productQuantity strong').html();
    var stock = parseInt(stockStr);

    if(stock <= 5) {

      $('#productWrapper input:checked').parent('.quantityChoice').find('.productQuantity).show();

      $('#productWrapper input').change(function() {

        var stockStr = $('#productWrapper input:checked').parent('.quantityChoice').find('.productQuantity strong').html();
        var stock = parseInt(stockStr);
        $('.productQuantity').hide();

        if(stock <= 5){

          $('#productWrapper input:checked').parent('.quantityChoice').find('.productQuantity).show();

        }

      });
    }




    <p id="productWrapper">
      <label>Choisissez :</label><br>
      <span class="quantityChoice">
        <input checked="checked" type="radio" name="" id="" value="">
        <label>Taille L</label>
        <span class="productQuantity">Il ne reste plus que <strong>1</strong> en stock ! </span>
      </span>      
      <span class="quantityChoice">
        <input type="radio" name="" id="" value="">
        <label>Taille XL</label>
        <span class="productQuantity">Il ne reste plus que <strong>15</strong> en stock ! </span>
      </span>                   
    </p>





Par avance merci !!
Modifié par Luc-F (24 Oct 2012 - 17:20)
Une piste étant d'enregistrer tes recherche dans une variable
Inutile à première vue d'aller recherche à chaque fois #productWrapper