Forum Moderators: open

Message Too Old, No Replies

jquery and javascript problem.

javascript problem

         

phplearner

8:05 am on Mar 17, 2011 (gmt 0)

10+ Year Member



The following code work properly in html.


<div id="suggestions" align="center">

function getSuggestions(value){
if(value != ""){
$.post("suggestion.php",{suggestionPart:value},function(data){
$("#suggestions").html(data);
});
}
}

<input type="text" id="searchfield" class="inputtext" name="searchfield" onKeyUp="getSuggestions(this.value);" value="" />


But it does not work in javascript. i would like to echo it in javascript code. i have mentioned it as below.


SearchField
written by Alen Grakalic, provided by Css Globe

this.searchfield = function(){

// CONFIG

// this is id of the search field you want to add this script to.
// You can use your own id just make sure that it matches the search field in your html file.
var id = "searchfield";

// Text you want to set as a default value of your search field.
var defaultText = "Search the word...";

// set to either true or false
// when set to true it will generate search suggestions list for search field based on content of variable below
var suggestion = true;

// static list of suggestion options, separated by comma
// replace with your own

var suggestionText = " <div id="suggestions" align="center"> ";

// END CONFIG (do not edit below this line, well unless you really, really want to change something :) )

// Peace,
// Alen

var field = document.getElementById(id);
var classInactive = "sf_inactive";
var classActive = "sf_active";
var classText = "sf_text";
var classSuggestion = "sf_suggestion";
this.safari = ((parseInt(navigator.productSub)>=20020000)&&(navigator.vendor.indexOf("Apple Computer")!=-1));
if(field && !safari){
field.value = defaultText;
field.c = field.className;
field.className = field.c + " " + classInactive;
field.onfocus = function(){
this.className = this.c + " " + classActive;
this.value = (this.value == "" || this.value == defaultText) ? "" : this.value;
};
field.onblur = function(){
this.className = (this.value != "" && this.value != defaultText) ? this.c + " " + classText : this.c + " " + classInactive;
this.value = (this.value != "" && this.value != defaultText) ? this.value : defaultText;
clearList();
};
if (suggestion){

var selectedIndex = 0;

field.setAttribute("autocomplete", "off");
var div = document.createElement("div");
var list = document.createElement("ul");
list.style.display = "none";
div.className = classSuggestion;
list.style.width = field.offsetWidth + "px";
div.appendChild(list);
field.parentNode.appendChild(div);

field.onkeypress = function(e){

var key = getKeyCode(e);

if(key == 13){ // enter
selectList();
selectedIndex = 0;
return false;
};
};

field.onkeyup = function(e){

var key = getKeyCode(e);

switch(key){
case 13:
return false;
break;
case 27: // esc
field.value = "";
selectedIndex = 0;
clearList();
break;
case 38: // up
navList("up");
break;
case 40: // down
navList("down");
break;
default:
startList();
break;
};
};

this.startList = function(){
var arr = getListItems(field.value);
if(field.value.length > 0){
createList(arr);
} else {
clearList();
};
};

this.getListItems = function(value){
var arr = new Array();
var src = suggestionText;
var src = src.replace(/, /g, ",");
var arrSrc = src.split(",");
for(i=0;i<arrSrc.length;i++){
if(arrSrc[i].substring(0,value.length).toLowerCase() == value.toLowerCase()){
arr.push(arrSrc[i]);
};
};
return arr;
};

this.createList = function(arr){
resetList();
if(arr.length > 0) {
for(i=0;i<arr.length;i++){
li = document.createElement("li");
a = document.createElement("a");
a.href = "javascript:void(0);";
a.i = i+1;
a.innerHTML = arr[i];
li.i = i+1;
li.onmouseover = function(){
navListItem(this.i);
};
a.onmousedown = function(){
selectedIndex = this.i;
selectList(this.i);
return false;
};
li.appendChild(a);
list.setAttribute("tabindex", "-1");
list.appendChild(li);
};
list.style.display = "block";
} else {
clearList();
};
};

this.resetList = function(){
var li = list.getElementsByTagName("li");
var len = li.length;
for(var i=0;i<len;i++){
list.removeChild(li[0]);
};
};

this.navList = function(dir){
selectedIndex += (dir == "down") ? 1 : -1;
li = list.getElementsByTagName("li");
if (selectedIndex < 1) selectedIndex = li.length;
if (selectedIndex > li.length) selectedIndex = 1;
navListItem(selectedIndex);
};

this.navListItem = function(index){
selectedIndex = index;
li = list.getElementsByTagName("li");
for(var i=0;i<li.length;i++){
li[i].className = (i==(selectedIndex-1)) ? "selected" : "";
};
};

this.selectList = function(){
li = list.getElementsByTagName("li");
a = li[selectedIndex-1].getElementsByTagName("a")[0];
field.value = a.innerHTML;
clearList();
};

};
};

this.clearList = function(){
if(list){
list.style.display = "none";
selectedIndex = 0;
};
};
this.getKeyCode = function(e){
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
return code;
};

};

// script initiates on page load.

this.addEvent = function(obj,type,fn){
if(obj.attachEvent){
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn](window.event );}
obj.attachEvent('on'+type, obj[type+fn]);
} else {
obj.addEventListener(type,fn,false);
};
};
addEvent(window,"load",searchfield);


any volunteer plz help me.

daveVk

12:55 pm on Mar 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="text" id="searchfield" class="inputtext" name="searchfield" onKeyUp="getSuggestions(this.value);" value="" />

...

field.onkeyup = function(e){
...

The first onkeyup is being overridden by the second

You could try removing the first and add the code within the second

say

field.onkeyup = function(e){
var key = getKeyCode(e);

switch(key){
case 13:
return false;
break;
case 27: // esc
field.value = "";
selectedIndex = 0;
clearList();
break;
case 38: // up
navList("up");
break;
case 40: // down
navList("down");
break;
default:
getSuggestions(this.value); // dont do on esc,up,down ?
startList();
break;
};
};

phplearner

2:59 pm on Mar 19, 2011 (gmt 0)

10+ Year Member



I have still problem in my script code.
The following code is working in html or php.

<div id="suggestions" align="center">

function getSuggestions(value){
if(value != ""){
$.post("suggestion.php",{suggestionPart:value},function(data){
$("#suggestions").html(data);
});
}
}

<input type="text" id="searchfield" class="inputtext" name="searchfield" onKeyUp="getSuggestions(this.value);" value="" />


but not working in javascript on location below.


var suggestionText = " <div id="suggestions" align="center"> ";

Fotiman

3:36 pm on Mar 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var suggestionText = " <div id='suggestions' align='center'> ";

You can't have double quotes within a double quoted string.

daveVk

1:11 am on Mar 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Single quotes are OK for attributes, but for consistency I would use.

var suggestionText = ' <div id="suggestions" align="center"> ';

phplearner

5:53 am on Mar 20, 2011 (gmt 0)

10+ Year Member



I have still problem in my script code.
Actually this variable in javascript function and in my head section.

var suggestionText = " <div id='suggestions' align='center'> ";

Help me.

daveVk

8:07 am on Mar 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




// static list of suggestion options, separated by comma
// replace with your own

var suggestionText = " <div id="suggestions" align="center"> ";



By the comment I assume the code expects something more like

var suggestionText = "red,blue,green";

What error indication do you get ?

phplearner

8:57 am on Mar 20, 2011 (gmt 0)

10+ Year Member



Dear daveVk,
Thank you for your help.
I couldn't let you understand my problem properly. my problem is when i put this code <div id="suggestions" align="center"> in html it works properly and output is like (red,blue,green).
but when i put the same code in JavaScript function


this.searchfield = function(){

var id = "searchfield";

var defaultText = "Search the word...";

var suggestion = true;

var suggestionText = " <div id="suggestions" align="center"> ";
}


it doesn't give any output.

Fotiman

9:53 pm on Mar 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




var suggestionText = " <div id="suggestions" align="center"> ";

Again, you can't have double quotes within a double quoted string. See my previous reply.

daveVk

12:28 am on Mar 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am still not sure I understand the problem.

I think div id="suggestions" contains the suggestions like (red,blue,green).

I think that the contents of this div needs to be transfered to the variable suggestionText.

so you could try

var suggestionText = $("#suggestions").html();

as you update $("#suggestions") from the server, you will need to update suggestionText as well.

function getSuggestions(value){
if(value != ""){
$.post("suggestion.php",{suggestionPart:value},function(data){
suggestionText = data;
$("#suggestions").html(data);
});
}
}