Hazel Mahmud

Hazel Mahmud

  • NA
  • 299
  • 65.6k

textbox length according to text

May 27 2015 3:23 AM
i have code that dynamically addtextbox but when i add onkeyup function, it work in Chrome and Firefox but it doesn't work in IE.. code that does not work is after the text with red colour. TQ so much.
 
$(document).ready(function () {

var counter = 2;

$("#btnAdd").click(function () {

if (counter > 4) {

return false;
}

var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);


newTextBoxDiv.after().html('<input type="text" name="textbox' + counter + '" size="1' +
'" onkeyup="Expand(this);"' + '" id="textbox' + counter + '" value="">');

newTextBoxDiv.appendTo("#LinkTab");

$('#textbox' + counter).focus();
counter++;
}); 
 
code below does not work in IE.. help me how to fix it anyone.
 
function Expand(obj) {

if (!obj.startW) { obj.startW = obj.offsetWidth; }

var style = obj.style;

//Force complete recalculation of width
//in case characters are deleted and not added:
style.width = 0;

var desiredW = obj.scrollWidth;

desiredW += obj.offsetHeight;

style.width = Math.max(desiredW, obj.startW) + 'px';



}

Answers (5)