How to handle click events on div

Pavel Filimonov

I have a data set of items coming from a SQL database. Those items are displayed in multiple divs like:

 <?php
 $url = 'DataBase';

 $json_response = file_get_contents ( $url );

 $json_arr = json_decode ( $json_response, true );

 for($i = count ( $json_arr )-1; $i > 0;  $i--) {
 $json_obj = $json_arr [$i];


 echo '<div id="MyDIV">';
 echo $json_obj ['id'] ;
 echo $json_obj ['title'];  
 echo $json_obj ['article'];  
 echo '<button id="read_more_btn">Read more</button>';
 echo '</div>'
 }
 ?>

My problem is that I cannot handle click events for EACH div, so I cannot identify which item has been clicked. I’ve been searching for a solution for quite a while, but haven’t found anything. So my question is – how can I identify the clicked item?

EDIT I have no idea how I can dynamically assign an ID to a button

geedubb

You could use a data attributes (HTML5 assumed) to attach the Id as meta data to your divs. Your PHP (note I am adding data-id attributes):

<?php
 $url = 'DataBase';
 $json_response = file_get_contents ( $url );
 $json_arr = json_decode ( $json_response, true );
 for($i = count ( $json_arr )-1; $i > 0;  $i--) {
   $json_obj = $json_arr [$i];
   echo '<div data-id="' . $json_obj['id'] . '">';
   echo $json_obj ['id'] ;
   echo $json_obj ['title'];  
   echo $json_obj ['article'];  
   echo '<button id="read_more_btn">Read more</button>';
   echo '</div>'
 }
 ?>

JS - simple click handler attachment, using jQuery .data() [docs] to get data attribute:

$('div').click(function(e) {
    var id = $(this).data("id");
    alert(id);
});

JSFiddle Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to handle click events with @Directive?

From Dev

How to Handle Multiple Click Events In Android?

From Dev

How to handle button click events in meteor?

From Dev

How to handle button click events in meteor?

From Dev

How to handle click events inside bootstrap button dropdown

From Dev

How to Handle events in Java?

From Dev

How to better handle events

From Dev

How to handle events with a webapp?

From Dev

excel VBA how to click on onclick events of a div on IE11

From Dev

How to assign two separate events to a div tag on click

From Dev

How could i target a div and it's child to handle a click?

From Dev

Stacking DIV Click Events for Javascript

From Dev

Stacking DIV Click Events for Javascript

From Dev

Best way to handle MenuItem Click events?

From Dev

Can an Actor handle click and key down events?

From Dev

NSButton in Swift, handle click and release events

From Dev

NSButton in Swift, handle click and release events

From Dev

How do I prevent any button click events from queuing until the event handle is finished with the first call

From Dev

How to handle click events on child components in React.js, is there a React "way"?

From Dev

How can I make a single event handler handle ALL Button.Click events?

From Dev

How to emit and handle custom events?

From Dev

How to handle form closing events

From Dev

How to handle Touch Events on a Fragment?

From Dev

How to handle events of a Frame Scrollbar?

From Dev

How to handle Touch Events on a Fragment?

From Dev

Using click events to change the div in "focus"?

From Dev

How to print click events in Javascript

From Dev

How can i remove a class from div using click events on specific elements?

From Dev

Handle click events when parent view has a touch listener

Related Related

  1. 1

    How to handle click events with @Directive?

  2. 2

    How to Handle Multiple Click Events In Android?

  3. 3

    How to handle button click events in meteor?

  4. 4

    How to handle button click events in meteor?

  5. 5

    How to handle click events inside bootstrap button dropdown

  6. 6

    How to Handle events in Java?

  7. 7

    How to better handle events

  8. 8

    How to handle events with a webapp?

  9. 9

    excel VBA how to click on onclick events of a div on IE11

  10. 10

    How to assign two separate events to a div tag on click

  11. 11

    How could i target a div and it's child to handle a click?

  12. 12

    Stacking DIV Click Events for Javascript

  13. 13

    Stacking DIV Click Events for Javascript

  14. 14

    Best way to handle MenuItem Click events?

  15. 15

    Can an Actor handle click and key down events?

  16. 16

    NSButton in Swift, handle click and release events

  17. 17

    NSButton in Swift, handle click and release events

  18. 18

    How do I prevent any button click events from queuing until the event handle is finished with the first call

  19. 19

    How to handle click events on child components in React.js, is there a React "way"?

  20. 20

    How can I make a single event handler handle ALL Button.Click events?

  21. 21

    How to emit and handle custom events?

  22. 22

    How to handle form closing events

  23. 23

    How to handle Touch Events on a Fragment?

  24. 24

    How to handle events of a Frame Scrollbar?

  25. 25

    How to handle Touch Events on a Fragment?

  26. 26

    Using click events to change the div in "focus"?

  27. 27

    How to print click events in Javascript

  28. 28

    How can i remove a class from div using click events on specific elements?

  29. 29

    Handle click events when parent view has a touch listener

HotTag

Archive