I have been using the live jquery method for a long time and it has been working so far, however I read that the live method i deprecated and replaced by "on".
I have a couple of headers all belonging to the same class but with different IDs. When clickng one of the headers I want to open up a dialog box. In the examples below I have replaced the jquery dialog opening with a simple alert. The html code for the page is generated dynamically using php.
For the record, I am receiving an error in the javascript console in Chrome. It says Uncaught TypeError: undefined is not a function jquery:16. Also, I am calling the code below from a function that is called from document ready.
This works fine.
$(".antennaCursor").live("click", function() {
alert("Hi");
});
This doesn't work
$(".antennaCursor").on("click", function() {
alert("Hi");
});
Nor does this one.
$(document).live("click", ".antennaCursor", function() {
alert("Hi");
});
This does not work.
I am including the latest jquery version. http://code.jquery.com/jquery-1.11.2.js and http://code.jquery.com/ui/1.11.2/jquery-ui.min.js
Does anyone have any advice how to fix this?
Edit: Updated the code here because it was missing some ending parentheis and semi colon. The real code was correct, though.
Here is how the code is called.
$(document).ready(function() {
handleTheClickEvent();
});
function handleTheClickEvent() {
$(".antennaCursor").on("click",function() {
alert("Hi");
});
}
Edit: Working with the code I have realized that there may be other problems with the code that are the root cause to the problems with the "on" method. Here is an update to the code. The exception handling throws an error randomly saying "TypeError: undefined is not function". Could there be a problem in the code that is causing the "on" method problem?
$( ".antennaCursor" ).live("click",function() {
try {
var data_id = $(this).attr("id");
url = "http://192.168.0.10/wiki/GetInformation.php?data1"+data_id+"&type=somedata";
dial=$("<div id='dialog2' style='text-align:left; width:auto; overflow:auto'></div>");
dial.attr("title","Data information: " + $(this).attr("id"));
dial.html('Fetching data! <img id="graph" src="/wiki/progress.gif" />');
dial.dialog({ position: "left"});
dial.dialog( "option", "width", 600 );
$.get(url,{
getmacroinfo : $(this).attr("id"), infotype : "draw"
}, function(data){
if ( $( "#accordion" ).data("ui-accordion") ) {
$( "#accordion").accordion("destroy");
$( "#accordion").empty();
$( "#accordion").remove();
}
dial.html(data);
});
dial.dialog( "open" );
return false;
} catch (err) {
alert(err);
}
});