Why can't I activate the event of click on this function (can't alert Done!)?
$("#content_tc p").click(function()
{
alert("Done!");
operacao = "4";
$.ajax(
{
type: "POST",
url: "php/tc.php",
data:
{
operacao: "4"
},
dataType: "html"
})
.done(function(msg)
{
$("#content_tc").append(msg);
})
.fail(function()
{
$("#content_tc").text("Error");
});
});
The code above is a file named tc.js
In the html page, where the script is called (p in div id="content_tc"
is where I need to click):
<!DOCTYPE html>
<html>
<head>
<link href="Content/css/styles.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/tc.js"></script>
</head>
<body>
<div class="central">
<div id="content_tc"><p>a</p></div>
</div>
</body>
</html>
Please, help me. Thanks.
Try to wrap your code inside DOM ready handler $(document).ready(function() {...})
or shorter form $(function() {...});
to make sure all of your DOM elements are loaded properly before executing your javacript code:
$(function() {
// Your code here
});
If your elements are dynamically created, you can use event delegation:
$('body').on('click', '#content_tc p', function() {
});