jQuery execute more than once on page, each(function() ?

Discussion in 'jQuery' started by theblade24, Mar 22, 2013.

  1. #1
    Hi, I have the below code which I believe basically says

    if the div with class="sectionavailability" equals "in stock" replace with SOME HTML or if it has "out of stock" replace with SOME OTHER HTML

    Works great, but only executes once on a page, the first occurance of class="sectionavailability"
    if(jQuery(".sectionavailability").length){     
          if(jQuery(".sectionavailability").text().toLowerCase() === "in stock"){
          jQuery(".sectionavailability").html("SOME HTML CODE HERE");
          }
          else if(jQuery(".sectionavailability").text().toLowerCase() === "out of stock"){
          jQuery(".sectionavailability").html("SOME OTHER HTML CODE HERE"); }
    }
    Code (markup):

    The problem is, I need this to execute on a product category page which will list an array of many products and class="sectionavailability" will occur many times.

    How does the above code need to be altered to have it execute more than once on a page, whenever class="sectionavailability" is found?

    I know perhaps this is involved each(function() ? But I have no idea how to alter the code to include it....

    Can someone help make the above if statement work more than once on a page please?

    Thanks!
     
    theblade24, Mar 22, 2013 IP
  2. bc1

    bc1 Active Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    Hi :) I think you want something like this...

    jQuery(".sectionavailability").each(function(){
        // your code here
        if(jQuery(this).length){
            if(jQuery(this).text().toLowerCase() === "in stock"){
                jQuery(this).html("SOME HTML CODE HERE");
            }
            else if(jQuery(this).text().toLowerCase() === "out of stock"){
                jQuery(this).html("SOME OTHER HTML CODE HERE"); }
            }
        }
    });
    Code (markup):
    So really you just replace jQuery(".sectionavailability") with jQuery(this) inside the each function.

    I don't have time to check I got that right but it's in the jQuery docs if what I wrote doesn't work. Good luck with it!
     
    Last edited: Apr 24, 2013
    bc1, Apr 24, 2013 IP
  3. solomon.flik

    solomon.flik Member

    Messages:
    22
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #3
    can any one here know how to create Facebook Like Title alert in Jquery
     
    solomon.flik, May 15, 2013 IP