jqueryphalconmetro-ui-css

jQuery onclick handler doesn't work


I am using MetroUI CSS to create a ListView from database rows :

<?php

use Phalcon\Mvc\Controller;

class loadReservationTablesController extends Controller
{

    public function indexAction(){
        $request = $this->request;
        if ($request->isPost() == true) {
            if ($request->isAjax() == true) {
                $critere = array();
                $critere['salle_code'] = $_POST['salle_code'];
                $ret = ReservationTable::lireParCritere($critere);
                if ($ret->count() > 0) {
                    $html = '';
                    foreach ( $ret as $key => $val ) {
                        $html .= '<div class="list">
                                    <div class="list-content" id="table_'.$ret[$key]->table_code.'">
                                        <span class="list-subtitle">'.$ret[$key]->table_lib.'</span>
                                        <span class="list-remark">'.$ret[$key]->reserver.'</span>
                                    </div>
                                  </div>';
                    }
                    echo $html;
                }
                else {
                    echo '<br/><div class="sub-header">No records to display</div>';
                }
            }
        }
        $this->view->disable();
    }

}

In the view :

$(document).ready(function() {

  $("div[id^='table_']").on("click", function(){
    alert("uuuuuu");
  });

});

At runtime the alert doesnt show , and the console contains no error ; even logfile has no errors. When I right-clicked the page to see its source code then the records are not in ! So how to fix it ?


Solution

  • Since its a dynamically added element add events through event delegation:

    $(document).ready(function() {
        $(document).on("click","div[id^='table_']", function(){
                alert("uuuuuu");
        });
    });