Well, the fact that you are trying to add a class to a function argument with dotted notation shows that you need to know more about how Javascript works than can be explained in a simple StackOverflow answer, but let me try:
input in readURL(input) is an argument name similar to a variable name, so you cannot extend it. This is a link to what you pass when you call readURL , in this case, if you look at the end of your code, this is a link to this . this refers to the elements referenced by the jQuery selector you passed in: "#imgInp" .
So, if you want to use the .joint class instead, just pass this as a jQuery selector:
$(".joint").change(function(){ readURL(this); });
- However, if you have multiple
.joint elements, your readURL function readURL stop working correctly, because currently it is only intended to change src in #blah . What you need to do is change the src each element matching img .
So, if you have this kind of HTML:
<input class="joint" type='file' /> <img class="joint-img" style="width:45px" src="#" alt="your image" /> <input class="joint" type='file' /> <img class="joint-img" style="width:45px" src="#" alt="your image" /> <input class="joint" type='file' /> <img class="joint-img" style="width:45px" src="#" alt="your image" />
You can place your readURL function after all the elements and change the reader.onload section as follows:
function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $(input).next('.joint-img').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); } } $(".joint").change(function(){ readURL(this); });
mVChr
source share