Jump to content

OnKeyup to update div and count maxlength in <textarea>


StefanRSA

Recommended Posts

I am trying to update a div and count maximum characters in a <textarea>.

I am not sure about my script. The div gets updated but the max characters is ignored.

Please help....

 

My script:

<script>
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}
function SmLink(strURL) {
//////////////  TEXTAREA MAX COUNT START
var mlength=strURL.getAttribute? parseInt(strURL.getAttribute("maxlength")) : ""
if (strURL.getAttribute && strURL.value.length>mlength)
strURL.value=strURL.value.substring(0,mlength)
//////////////  TEXTAREA MAX COUNT END

	var req = getXMLHTTP();
	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {			
					document.getElementById('smlink').innerHTML=req.responseText;						
				} else {
					alert("Howzit!!! You did it wrong! \n" + req.statusText);
				}
			}				
		}			
		req.open("GET", strURL, true);
		req.send(null);
	}
}
</script>
<textarea name="bodyt" id="blink" maxlength="20" onkeyup="SmLink('<?=$root;?>/member/smbuild.php?bodyt='+this.value)"></textarea>
<div id='smlink'></div>

 

Ok... Let me try and explain what I am trying to do.... If anybody can help me it would be nice.

I have a textarea where a user can type a comment. I want to use a div to display a preview as the user is typing. This textarea should be limited to say 60 characters...

 

How should I do this?

I was trying to combine the div update and maxlength in one JS function but its not working.

Any suggestions?

Try changing this:

 

var mlength=strURL.getAttribute? parseInt(strURL.getAttribute("maxlength")) : ""
if (strURL.getAttribute && strURL.value.length>mlength)
strURL.value=strURL.value.substring(0,mlength)

 

to this:

 

var mlength=document.getElementById("myText").maxLength;
if (strURL.length>mlength)
strURL=strURL.substring(0,mlength);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.