-4

I have a php file:

$sql = mysql_query("SELECT id, name FROM table") or die(mysql_error());
if (mysql_num_rows($sql) > 0){
    while($row = mysql_fetch_array($sql)){
      echo "<div class=\"content-row\">
                <div class=\"row-1\">".$row['id']."</div>
                <div class=\"row-2\">".$row['name']."</div>
            </div>;
   }
}

In JS file:

success: funxction(data){ $(".content").html(data); }

In HTML file I have:

<div class="content">
    // Here the ajax put the divs
</div>

How can I get the content of <div class="row-1"></div> or <div class="row-2"></div> when I click on a button? The button is situated out of .content area. I clicked on a row, it is lighted and I click on button. When a clicked on button, I want to get the id and name div content where the row is lighted.

............................................. Update: I have this js code: http://pastebin.com/bjU8Kp3L

when I click on the button, check if a row was selected; if is not selected i receive an notification, if the row is selected i want to receive div content :)

Ovidiu
  • 1
  • 3

1 Answers1

1

You have to use event-delegation for dynamic created elements,

Bind the click-handler on a static element, and delegate the event to you dynamic ones:

$('.content').on('click', '.content-row > div', function(){

    $(this).text();
    //OR
    $(this).html();

});

Demo

empiric
  • 7,825
  • 7
  • 37
  • 48
  • Sorry , I forgot to specify : I want to take The content of the div when I click on a button that no row: – Ovidiu Apr 10 '15 at 07:09
  • @Ovidiu Where is this button ? Can you please edit your question to add this information. – empiric Apr 10 '15 at 07:09
  • Your example is not working because i have generated these divs with php. I have try this – Ovidiu Apr 10 '15 at 07:20