[code]enhance textarea (Select Code/Expand)

Tải code bên dưới tại đây code_scriptjs

function enhancetextarea(classname){
var alltextareas=document.getElementsByTagName(“textarea”)
for (var i=0; i<alltextareas.length; i++){
if (alltextareas[i].className==”codecontainer”){
var curtextarea=alltextareas[i]

var helperbar=document.createElement(“div”)
helperbar.innerHTML='<div class="selectcode"><a href="#selectcode" class="tabs">Select Code</a> <a href="#expandcode" class="tabs">Expand</a></div>'
var helperbarlinks=helperbar.getElementsByTagName(“a”)
if (curtextarea.offsetHeight>=curtextarea.scrollHeight){
helperbarlinks[1].style.display=”none”
curtextarea.style.overflowY=”hidden”
}

helperbarlinks[0].setAttribute(“rel”, i)

helperbarlinks[0].onclick=function(){
var targettextarea=document.getElementsByTagName(“textarea”)[parseInt(this.rel)]
targettextarea.select()
return false
}

helperbarlinks[1].setAttribute(“rel”, i)
helperbarlinks[1].setAttribute(“rev”, curtextarea.offsetHeight+” defaultHeight”)

helperbarlinks[1].onclick=function(){
var targettextarea=document.getElementsByTagName("textarea")[parseInt(this.rel)]
if (this.getAttribute("rev").indexOf("defaultHeight")!=-1){// if textarea is default height
targettextarea.style.height=targettextarea.scrollHeight+"px"
this.innerHTML="Contract"
targettextarea.style.overflowY="hidden"
targettextarea.style.borderStyle="solid"
this.setAttribute("rev", this.getAttribute("rev").replace("defaultHeight", "scrollHeight"))
if (this.scrollIntoView)
this.scrollIntoView()
return false
}
else{
targettextarea.style.height=parseInt(this.getAttribute("rev"))+"px"
this.innerHTML="Expand"
targettextarea.style.overflowY="scroll"
targettextarea.style.borderStyle="dashed"
this.setAttribute("rev", this.getAttribute("rev").replace("scrollHeight", "defaultHeight"))
}

return false
}
curtextarea.style.marginTop=0
curtextarea.parentNode.insertBefore(helperbar, alltextareas[i])
}
}
}

window.onload=function(){
enhancetextarea()
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: