Jump to content

Emulating div contentEditable = "true" with textarea


jimmyoneshot

Recommended Posts

I'm trying to emulate exactly how a div with the attribute contentEditable = "true" would operate using a textarea...trying and failing. I found some code somewhere which attempts to do something like his but it isn't working well for me.

 

I want to do this without jquery and with javascript only. The best example I can think of is Facebook's text area that you use to write on somebody's wall.

 

Here's my sizing function

 


function sz(t) {
a = t.value.split('\n');
b=1;
for (x=0;x < a.length; x++) { if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
}
b+= a.length;
if (b > t.rows) t.rows = b;
}


 

and here's my text area:-

 


<textarea id="text_entry" rows="1" cols="1" onkeyup="sz(this)" >User text goes here</textarea>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.