StefanRSA Posted September 20, 2010 Share Posted September 20, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/213914-onkeyup-to-update-div-and-count-maxlength-in/ Share on other sites More sharing options...
StefanRSA Posted September 20, 2010 Author Share Posted September 20, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/213914-onkeyup-to-update-div-and-count-maxlength-in/#findComment-1113427 Share on other sites More sharing options...
gamesmstr Posted September 20, 2010 Share Posted September 20, 2010 Are you looking to limit it to a defined integer? Or are you trying to limit it to the maxlength of the textarea? Your textarea has a limit of 20 characters. I'm wondering where the 60 you are wanting to limit it to is coming from. Quote Link to comment https://forums.phpfreaks.com/topic/213914-onkeyup-to-update-div-and-count-maxlength-in/#findComment-1113486 Share on other sites More sharing options...
StefanRSA Posted September 21, 2010 Author Share Posted September 21, 2010 Sorry, 60 or 20... I just want to limit the characters in the textarea... The maxlength of the text area will be where it will be set Quote Link to comment https://forums.phpfreaks.com/topic/213914-onkeyup-to-update-div-and-count-maxlength-in/#findComment-1113621 Share on other sites More sharing options...
gamesmstr Posted September 22, 2010 Share Posted September 22, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/213914-onkeyup-to-update-div-and-count-maxlength-in/#findComment-1113959 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.