Jump to content

Recommended Posts

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

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.