SteveAustin
SteveAustin

Reputation: 85

Using each function

Multiple products per page. I want to display message based on quantity.

$j(document).ready(function () {
    $j('#stockqty').text((parseInt($j('#stockqty').text()) > 0) ? "In Stock" : "Out of Stock");
});

I've tried this:

$j(document).ready(function () {
    $j('#stockqty').each(function () {.text((parseInt($j('#stockqty').text()) > 0) ? "In Stock" : "Out of Stock");
    });
});

Just not working.

Upvotes: 0

Views: 65

Answers (1)

Josh Beam
Josh Beam

Reputation: 19772

$(function () {
    $('.stockqty').each(function () {
        parseInt($(this).text()) > 0) ? return "In Stock" : return "Out of Stock");
    });
});

Also, there should not be multiple elements with the ID of "stockqty". Consider changing that to class="stockqty" (subsequently using the corresponding jQuery selector).

Additionally, although I have used a return statement in the above code, it is possible that you might want to instead append a value to the user interface. You can do this through something like:

$('#resultArea').html("In Stock") : $('#resultArea').html("Out of Stock");

Lastly, it's perfectly acceptable to just do

$(function() {

instead of

$(document).ready(function() {



(Note: the commonly used jQuery symbol is "$", unless you previously passed in "$j" as a parameter.)

Upvotes: 1

Related Questions