0

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.

5
  • What is $j. Maybe to put just $ Commented Jan 3, 2014 at 20:25
  • .text in your .each callback needs to be called on something. Commented Jan 3, 2014 at 20:25
  • 2
    $j('#stockqty').each ?? Duplicating the id? Commented Jan 3, 2014 at 20:26
  • 1
    $("#someid").each doesn't make sense, there can be only one $("#someid") Commented Jan 3, 2014 at 20:26
  • $('#stockqty') will only return one element. What are you trying to do? Commented Jan 3, 2014 at 20:27

1 Answer 1

1
$(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.)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that all makes perfect sense. I should of realized the ID thing, but still no joy. Fiddler seems to have a problem with it also.
Can you post a link to your fiddle?
I still saw a few syntax issues with your code (in addition, there was no HTML). I chose to go with jQuery 1.10, as well, instead of what you were using (1.7). See this updated jsfiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.