Latest news
Home » jQuery » Html Textarea auto resize using javascript

Html Textarea auto resize using javascript

 

<script>

var observer;
if (window.attachEvent) {
observer = function (element, event, handler) {
element.attachEvent(‘on’+event, handler);
};
}
else {
observer = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
function init (maximumHeight) {
var text = document.getElementById(‘text’);
var maxHeighter=maximumHeight;
var olderHeight=  text.scrollHeight;
var newerHeight;
function resize () {

text.style.height = ‘auto’;
newHeight= text.scrollHeight;
if(newerHeight>olderHeight && newerHeight>maxHeighter  )
{
text.style.height=olderHeight+’px’;

}
else{
text.style.height = newerHeight+’px’;
oldHeight=  text.scrollHeight;
}

}
/* 0-timeout to get the already changed text */
function delayedResize () {
window.setTimeout(resize, 0);
}
observer(text, ‘keydown’, delayedResize);
observer(text, ‘change’,  resize);
observer(text, ‘paste’,   delayedResize);
observer(text, ‘drop’,    delayedResize);
observer(text, ‘cut’,     delayedResize);

text.focus();
text.select();
resize();
}
init(100);

</script>

 

Leave a Reply

x

Check Also

How to Check Expired Sessions using jQuery Ajax and PHP

We can achieve using the php and ajax.The following ajax call execute in every 10 ...