Wrap Text Inside Contenteditable Div In Every Row
I am working on to something like a C++ web-based IDE. Im having some problem when i tried to append a text file to my contenteditable div (open file button). The DOM structure MUS
Solution 1:
Can you try following...
function readSingleFile(evt) {
var f = evt.target.files[0];
//console.log(f);
if (!f) {
alert("Failed to load file");
return;
}
if (f.name.indexOf('.txt') == -1) {
alert(f.name + " is not a valid text file.");
return;
}
var r = new FileReader();
r.onload = function (e) {
var contents = e.target.result; //.replace("\r\n","<br/>");
contents = contents.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
alert("Got the file.n" + "name: " + f.name + "n" + "type: " + f.type + "n" + "size: " + f.size + " bytesn" + "contents: " + contents);
var tmpSent = "";
var newContents = "";
for (var i = 0; i < contents.length; i++) {
if(contents.charAt(i) == '\n') {
newContents += "<div>"+tmpSent+"</div>";
tmpSent = "";
}
else
tmpSent += contents.charAt(i);
};
if(tmpSent.length>0)
newContents += "<div>"+tmpSent+"</div>";
console.log(newContents);
document.getElementById('board').innerHTML = newContents;
}
r.readAsText(f);
}
document.getElementById('fileinput').addEventListener('change', readSingleFile, false);
Here's fiddle link...jsfiddle
Post a Comment for "Wrap Text Inside Contenteditable Div In Every Row"