robert.access Posted October 12, 2011 Share Posted October 12, 2011 Hi, please help me! I have the following js script that expand a search box with the names from a database, and I want to limit to let's say 20 chars. For example if I search for "audi" it display in a div "audi a6 model year 2006, fuel petrol" I want to display only "audi a6 model ye..." Would that be possible? Thank you in advance for any help! <script language="JavaScript" type="text/javascript"> /*-------------------------Start of Suggest Code-----------------------------*/ function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { alert("Your browser is so OLD!\nIt's about time to upgrade don't you think?"); } } //Our XmlHttpRequest object to get the auto suggest var searchReq = getXmlHttpRequestObject(); //Called from keyup on the search textbox. //Starts the AJAX request. function searchSuggest() { var str = escape(document.getElementById('basicsearch').value); if (searchReq.readyState == 4 || searchReq.readyState == 0) { searchReq.open("GET", 'searchSuggest.php?search=' + str, true); searchReq.onreadystatechange = handleSearchSuggest; searchReq.send(null); } } //Called when the AJAX response is returned. function handleSearchSuggest() { if (searchReq.readyState == 4) { var ss = document.getElementById('search_suggest') ss.innerHTML = ''; var str = escape(document.getElementById('basicsearch').value); if (str=='') { ss.style.visibility = 'hidden'; } else { ss.style.visibility = 'visible'; str = searchReq.responseText.split("\n"); if (str=='') ss.style.visibility = 'hidden'; for(i=0; i < str.length - 1; i++) { //Build our element string. This is cleaner using the DOM, but //IE doesn't support dynamically added attributes. var suggest = '<div onmouseover="javascript:suggestOver(this);" '; suggest += 'onmouseout="javascript:suggestOut(this);" '; suggest += 'onclick="javascript:setSearch(this.innerHTML);" '; suggest += 'class="suggest_link">' + str + '</div>'; ss.innerHTML += suggest; } } } } //Mouse over function function suggestOver(div_value) { div_value.className = 'suggest_link_over'; } //Mouse out function function suggestOut(div_value) { div_value.className = 'suggest_link'; } //Click function function setSearch(value) { document.getElementById('basicsearch').value = value; document.getElementById('search_suggest').innerHTML = ''; document.getElementById('search_suggest').style.visibility = 'hidden'; document.getElementById('auctionsearch').submit(); } /*-------------------------End of Suggest Code------------------------------*/ </script> Link to comment https://forums.phpfreaks.com/topic/248972-truncate-chars-and-replace-with/ Share on other sites More sharing options...
requinix Posted October 12, 2011 Share Posted October 12, 2011 There's a CSS 3 property text-overflow which most browsers already support... Link to comment https://forums.phpfreaks.com/topic/248972-truncate-chars-and-replace-with/#findComment-1278651 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.