Nicholas Murray
Nicholas Murray

Reputation: 13509

jQuery Retrieve the contents of an array of divs

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*);
});

Upvotes: 1

Views: 1500

Answers (3)

harpax
harpax

Reputation: 6116

$(".ProductName").each(function(k, v) {
    alert($(v).text());
});

Upvotes: 4

bang
bang

Reputation: 5221

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

Upvotes: 1

Adam Kiss
Adam Kiss

Reputation: 11859

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)

Upvotes: 0

Related Questions