I have this HTML and JS/jQuery code:
$(document).ready(function() {
$("#livesearch").on("click", ".searchres", function(event){
console.log('clicked');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="livesearch">
<?php foreach($data as $i){?>
<li class="searchres"><?php echo $i["name"];?></li>
<?php } ?>
</ul>
This is part of a dropdown list that appears after an input form is being populated.
I would like to press on one of the dropdown lists .searchres
and just print to console as a test.
This is, however, not printing on console. What am I doing wrong?
Details
At the start of my php file I have this code snippet that creates a 'livesearch' object which connects to my database.
<?php
require_once('livesearch.php');
$livesearch = new livesearch();
$data = null;
?>
I am declaring $data=null
here because I do not want any database fields to show at loading time. could this be the issue that is making livesearch Id non-existant on load?
` did not exist when the event handler is executed. If you insert the ul after document.ready, then you need to delegate from document and not from #livesearch
– mplungjan Jul 18 '19 at 09:50– mplungjan Jul 18 '19 at 09:55