this jquery code works fine for me in jquery format.
I want convert this jquery code to coffeescript code:
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.preview_browse img#thumb').attr('src', e.target.result).width(251).show();
};
reader.readAsDataURL(input.files[0]);
$('.not_found_image_browse').hide();
}
}
I get from http://js2coffee.org/ this code:
readURL = (input) ->
if input.files and input.files[0]
reader = new FileReader()
reader.onload = (e) ->
$(".preview_browse img#thumb").attr("src", e.target.result).width(251).show()
reader.readAsDataURL input.files[0]
$(".not_found_image_browse").hide()
but when I enter this code in my project rails in post.js.coffee does not works fine for me.
I get in browser console this error:
readURL is not defined
onchange()onchange (line 2)
event = change
[Break On This Error]
readURL(this);
This is my html code in my input file:
<input id="post_image" class="file required" type="file" onchange="readURL(this)" name="post[image]">
readURLisn't in the global scope, so theonchangedoesn't work.onchangeattribute when you have jQuery? Why not use jQuery to bind to the event?$('#post_image').change(function(){ readURL(this); });.