What is the difference between $('id or class name').click(function() vs $(document).on('click', '.id or classname', function(e)
.
- How to best use each one.
- What is the situation that use each one.
- What is the advantage of one over the other
I have a couple of examples where $(document).on('click', '.id or class-name', function()
works and $('id or class name').click(function()
did not work the most recent encounter is one during this project of mine where I generate a table with values then at the last TD i have an anchor with a class named add
. I made it a class so event if I have multiple generated row the click event will still fire but again using $('#add').click
didn't work while using $(document).on('click', '.id or class-name', function(e)
worked.
Also this is not same but using $('#file')
didn't work here so i just added it.
Getting the content of input file using id
var file = document.getElementById('file').files[0];
worked but var file = $('#file').files[0];
T check the file like this
var name = file.name;
var size = file.size;
var type = file.type;
console.log("name " + name);
console.log("size " + size);
console.log("type " + type);
The result of this is
name whomovedmycheese - Copy.pdf
size 500624
type application/pdf
I know that using $('# or . then name')
will select the id or class but why is it not working during these times. Some may just point me to document I read them but I don't think i fully understand all of them that is why I am confused on these thing. Any clarification is appreciated.