0
I have the following code:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$.getJSON('livros.json', function(data) {
$.each(data, function(i, item) {
console.log(item.name);
$("#lista").append("<li><a class='livro'>" + item.name + "</a></li>");
});
});
</script>
<script type="text/javascript" src="js/script.js"></script>
<body>
<div id="wrapper">
<ul id="lista">
</ul>
</div>
</body>
The code above I am listing my json and setando with the append
in html, so far everything is working, only now I want to catch the click action on each tag a
of html.
and I’m not getting it.
click script that is not working:
$(document).on('click', '.livro', function() {
console.log("foi");
});
Try the following code,
jQuery(document.body).on('click', '.livro', function (event) {
 console.log("foi");
});
If it works, I’ll explain it and add it to the answer.– Barbetta
that didn’t work either
– jose
Look at this https://jsfiddle.net/3a6p28u5/14/
– Barbetta
@Barbetta this way worked, thanks had not thought of this solution.
– jose
I’ll play on the answer.
– Barbetta
Possible duplicate of I can’t get the value of a child element
– Francisco