This is a simple my script jquery ajax populate navigation bar like choose category and subcategory after returning of data. here fist is (static) html unordered list and second is dynamic unordered list after returning data from ajax i want to use some jquery events like there.
1: first get li text from static unordered list. it is working ok.
$("#brands li").click(function(){
// This is working ok
var brand = $(this).text();
alert(brand);
2: and second is get text from dynamic unorderd list it is not working
$("#model li").click(function(){
var model = $(this).val();
alert(model);
});
(index.php page)
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>AJAX UL LIST</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
*{
margin:0px;
padding:0px;
}
.menu{
border:1px solid gray;
width:125px;
min-height:200px;
font-family:arial;
float:left;
margin-top:20px;
margin-left:20px;
padding:10px;
background-color:#E6E6E6;
box-sizing:border-box;
border-radius:3px;
}
.menu ul{
list-style:none;
}
.menu ul li{
margin-bottom:10px;
font-weight:bold;
color:#045FB4;
cursor:pointer;
}
</style>
</head>
<body>
<div class="menu">
<ul id="brands">
<li id="brand">Toyota</li>
<li id="brand">Suzuki</li>
<li id="brand">Dodge</li>
<li id="brand">Hyundai</li>
<li id="brand">chevrolet</li>
</ul>
</div>
<div class="menu">
<ul id="model">
</ul>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$("#brands li").click(function(){
// This is working ok
var brand = $(this).text();
alert(brand);
//
$.ajax({
url:"fetch_list.php",
method:"POST",
data:{brandName:brand},
dataType:"text",
success:function(data)
{
$('#model').html(data);
}
});
});
// This is not working
$("#model li").click(function(){
var model = $(this).val();
alert(model);
});
});
</script>
(fetch_list.php page)
<?php
$connect = mysqli_connect("localhost","root","","test");
$output = '';
$sql = "SELECT * FROM ul_li_ajax_test WHERE car_brand = '".$_POST["brandName"]."' ORDER BY car_names";
$result = mysqli_query($connect, $sql);
$output = '<li>Select Model</li>';
while($row = mysqli_fetch_array($result))
{
$output .= '<li>'.$row["car_names"].'</li>';
//$output .= '<li><a href=?model='.$row["car_names"].'>'.$row["car_names"].'</a></li>';
}
echo $ output;
?>