0

i have try different solutions from this media but still it does not work for me please help me.

function get_category(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var ans = document.getElementById("sell_panel");
ans.innerHTML  = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "includes/get_category.php", true);
xmlhttp.send();

}
`$('.item_btn').on('click', function () {// HERE IS MY CLICK EVENT
alert("helo");
});`

AND MY get_category.PHP FILE IS;

<?php
include("database.php");
$db = new Database();   
$sql = "SELECT DISTINCT category FROM stock";
$result = mysql_query($sql);
if($result === FALSE) { 
die(mysql_error()); // TODO: better error handling
}   
while($cat = mysql_fetch_array($result)){

echo "
<div class='item_btn' id='{$cat[0]}'>//HERE IS THE DIV

<label>{$cat[0]}</label>

</div>  
";
}   

?>

I HAVE TRIED delegate() also it did not work for me and i have heard about to trigger my event handler in ajax but i dont know how to do it please help me guys

2 Answers2

0

Try to attach event on parent instead of .item_btn:

$('body').on('click','.item_btn', function () {
   alert("helo");
});
Manwal
  • 23,450
  • 12
  • 63
  • 93
0

Which version of jQuery are you using if its older try .live() instead of .on()

$( selector ).live( events, data, handler );                // jQuery 1.3+
$( document ).delegate( selector, events, data, handler );  // jQuery 1.4.3+
$( document ).on( events, selector, data, handler );        // jQuery 1.7+
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44