Advertisement
keysle

K8r Box Primitive javascript

Oct 21st, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. function k8rBox( div ){
  2. var focused,box,activeText;
  3. var box = document.getElementById(div);
  4.  
  5. activeText = document.createElement('input');
  6.  
  7. activeText.setAttribute('type','text');
  8. activeText.onkeydown = function(){
  9. fitBoxToText();
  10. }
  11. activeText.onfocus = function(){ focused = 1;}
  12. activeText.onblur = function(){ focused = 0;}
  13.  
  14. if (activeText.style.fontSize == "")
  15. activeText.style.fontSize = "14px";
  16. if (activeText.style.fontWeight == "")
  17. activeText.style.fontWeight = "normal";
  18. if (activeText.style.fontVariant == "")
  19. activeText.style.fontVariant = "normal";
  20. if (activeText.style.fontStyle == "")
  21. activeText.style.fontStyle = "normal";
  22. if (activeText.style.fontFamily == "")
  23. activeText.style.fontFamily = "verdana";
  24.  
  25. box.appendChild(activeText);
  26.  
  27. this.bubbleUp = function(){
  28. repositionActiveText();
  29. }
  30.  
  31. this.repositionActiveText = function(){
  32.  
  33. }
  34.  
  35. box.onclick = function(){
  36. activeText.focus();
  37. };
  38.  
  39. this.fitBoxToText = function(){
  40.  
  41. var sizeTester = document.createElement('span');
  42. setTimeout(function(){
  43. sizeTester.innerHTML = activeText.value;
  44. sizeTester.style.position = 'absolute';
  45. sizeTester.style.left = "-19px";
  46. sizeTester.style.top = "-49px";
  47. sizeTester.style.fontSize = activeText.style.fontSize;
  48. sizeTester.style.fontWeight = activeText.style.fontWeight;
  49. sizeTester.style.fontVariant = activeText.style.fontVariant;
  50. sizeTester.style.fontStyle = activeText.style.fontStyle;
  51. sizeTester.style.fontFamily = activeText.style.fontFamily;
  52.  
  53. box.appendChild(sizeTester);
  54.  
  55. activeText.style.width = sizeTester.offsetWidth+10+"px";
  56.  
  57. box.removeChild(sizeTester);
  58. sizeTester = null;
  59.  
  60. },0001); // we're doing a delay because this fires before the actual value change
  61. }
  62.  
  63. fitBoxToText();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement