user2157179
user2157179

Reputation: 236

Get value inside dynamic DIVs

I am having difficulty getting the value inside of a set of dynamically created divs using jQuery. All divs are created with PHP and there are 6 different sets of dynamic divs. My code for the divs look like the following:

foreach ($data as $ad) 
{
   echo "<div class='rowDiv' id='" . $ad['name'] ."'>" . $ad['name'] . "</div>";
}

And here is how I am trying to retrieve the values inside of the div.

$('.rowDiv').click(function(){
    var name = $('.rowDiv').text();
    console.log(name);
});

However doing this ends up with showing every single value in all the divs with the same class name. How can I specify it to retrieve the value only in the div which has been clicked?

Upvotes: 0

Views: 494

Answers (1)

flochtililoch
flochtililoch

Reputation: 6206

Your click handler basically target every elements with .rowDiv class in the page.

Change your code to

$('.rowDiv').click(function(){
  var name = $(this).text();
  console.log(name);
});

to target only the clicked one

Upvotes: 1

Related Questions