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>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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