1

I have a collection of divs on a page with the same class name.

<div class="ProductName">Product Foo</div>
<div class="ProductName">Product Bar</div>

I would like to be able to retrieve, iterate through and this case alert the ProductName div's contents.

Currently I can retrieve and iterate, but I can't alert the individual contents.

var ExistingProductNamesOnscreen = $.makeArray($(".ProductName"));
$.each(ExistingProductNamesOnscreen, function (key, val) {
    alert(*ProductName contents*);
});
1
  • Thanks everyone for your more elegant solutions! Commented Feb 9, 2010 at 11:22

3 Answers 3

4
$(".ProductName").each(function(k, v) {
    alert($(v).text());
});
Sign up to request clarification or add additional context in comments.

Comments

1
$(".ProductName").each(function ()
{
    alert($(this).text());
});

Comments

0

did you try

alert ($(this).text());

or

alert ($(this).html());

?

(The first should alert text contents, while the latter also any tags found in particular div)

Comments

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.