SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Hi, I have the below code which I believe basically says

    if the div with id="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 id="sectionavailability"

    Code:
    		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");
    		}
    	}
    The problem is, I need this to execute on a product category page which will list an array of many products and id="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 id="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!

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    London
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "id"s should be unique on the page.
    perhaps you should move the "sectionavailability" into the class attribute instead.

  3. #3
    SitePoint Member Knight Yoshi's Avatar
    Join Date
    May 2012
    Location
    Tampa, Florida, United States
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to switching it to a class tag, as stated above
    Code:
    jQuery('.TheClassTag').each(function() {
         if(jQuery('.TheClassTag').length){
    		if(jQuery('.TheClassTag').text().toLowerCase() === "in stock"){
    			jQuery('.TheClassTag').html("SOME HTML CODE HERE");
    		}
    		else if(jQuery('.TheClassTag').text().toLowerCase() === "out of stock"){
    			jQuery('.TheClassTag').html("SOME OTHER HTML CODE HERE");
    		}
    	}
    })
    Should work


Tags for this Thread

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
  •