Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a code to impletement autocomplete in javascript which is leanthy but simple to understand

html:
HTML
<div class="sug">
	<input type="text" id="auto" onkeyup="display(event)" onblur="hide()" autocomplete="off" />
    <div class="suggestion" id="suggestion">
    </div>
    </div>



css:
CSS
.suggestion
	{
		border:solid 2px black;
	}
	.sug > .suggestion
	{
		display:none;
	}
	.sug > .suggestion,#auto
	{
		width:100px;
	}


javascript:
JavaScript
var array=new Array();
array.push("heena");
array.push("bhawin");
array.push("aruna");
array.push("mahesh");
array.push("anshul");
array.push("jagruti");
array.push("neha");
array.push("sherry");
array.push("sonali");
var data;
var id;//for providing id to each div
	function display(e)
	{
		if(e.keyCode==38 || e.keyCode==40)
		{
			selectit(e);
		}
		data="";
		id=0;
		var state=$('#auto').val();
		if(state=="")//value empty
		{
			$('.suggestion').css({display:"none"});
		}
		else
		{
			for(var i=0;i<array.length;i++)
			{
				var key=0;
				for(j=0;j<state.length;j++)
				{
					//for the matching of the array element with the text in the textbox
					if(array[i][j]==state[j])
					{
						key++;
					}
				}
				//putting the matched array element in a div
				if(key==state.length)
				{
					//the whole array will be copied with the bold values inserted
					var bolde="";
					for(var k=0;k<key;k++)
					{
						bolde+=""+array[i][k]+"";
					}
					for(var l=key;l<array[i].length;l++)
					{
						bolde+=array[i][l];
					}
					id++;
					data+="<div id='"+id+"'>"+bolde+"</div>";
				}
			}
			$('.suggestion').html(data);
			$('.suggestion').css({display:"block"});
			if(data=="")
			{
				$('.suggestion').css({display:"none"});
			}
		}
	}
	function hide()
	{
		$('#suggestion').css({display:"none"});;
	}
	function selectit(e)
	{
		 var child=document.getElementById("suggestion").childNodes;
		 for(var i=0;i<child.length;i++)
		 {
			 if(child[i].id=="1")
			 {
				 child[i].style.color="red";   //here is the problem in the code
			 }
		 }
	}


there is a problem at function line child[i].style.color="red"; the child is not coloured red, why?
please help me
Posted

1 solution

Please try following JavaScript code

C#
<script type="text/javascript">
        var array = new Array();
        array.push("heena");
        array.push("bhawin");
        array.push("aruna");
        array.push("mahesh");
        array.push("anshul");
        array.push("jagruti");
        array.push("neha");
        array.push("sherry");
        array.push("sonali");
        var data;
        var id; //for providing id to each div
        function display(e)
        {

            if (e.keyCode == 38 || e.keyCode == 40)
            {
                selectit(e);
            }
            data = "";
            id = 0;
            var state = $('#auto').val();
            if (state == "")//value empty
            {
                $('.suggestion').css({ display: "none" });
            }
            else
            {
                for (var i = 0; i < array.length; i++)
                {
                    var key = 0;
                    for (j = 0; j < state.length; j++)
                    {
                        //for the matching of the array element with the text in the textbox
                        if (array[i][j] == state[j])
                        {
                            key++;
                        }
                    }
                    //putting the matched array element in a div
                    if (key == state.length)
                    {
                        //the whole array will be copied with the bold values inserted
                        var bolde = "";
                        for (var k = 0; k < key; k++)
                        {
                            bolde += "" + array[i][k] + "";
                        }
                        for (var l = key; l < array[i].length; l++)
                        {
                            bolde += array[i][l];
                        }
                        id++;
                        data += "<div id='" + id + "'>" + bolde + "</div>";
                    }
                }
                $('.suggestion').html(data);
                $('.suggestion').css({ display: "block" });
                selectit(e);
                if (data == "")
                {
                    $('.suggestion').css({ display: "none" });
                }
            }
        }
        function hide()
        {
            $('#suggestion').css({ display: "none" }); ;
        }
        function selectit(e)
        {
            var child = document.getElementById("suggestion").childNodes;

            for (var i = 0; i < child.length; i++)
            {

                if (child[i].id == "1")
                {
                    child[i].style.color = "red";   //here is the problem in the code
                }
            }
        }
    </script>
 
Share this answer
 
Comments
bhawin parkeria 23-Aug-13 16:47pm    
this code just created color red all by itself not when i press the down key
Sandeep Singh Shekhawat 23-Aug-13 20:58pm    
please try following code that create color red of first dive when press down arrow key.

<script type="text/javascript">
var array = new Array();
array.push("heena");
array.push("bhawin");
array.push("aruna");
array.push("mahesh");
array.push("anshul");
array.push("jagruti");
array.push("neha");
array.push("sherry");
array.push("sonali");
var data;
var id; //for providing id to each div
function display(e)
{


data = "";
id = 0;
var state = $('#auto').val();
if (state == "")//value empty
{
$('.suggestion').css({ display: "none" });
}
else
{
for (var i = 0; i < array.length; i++)
{
var key = 0;
for (j = 0; j < state.length; j++)
{
//for the matching of the array element with the text in the textbox
if (array[i][j] == state[j])
{
key++;
}
}
//putting the matched array element in a div
if (key == state.length)
{
//the whole array will be copied with the bold values inserted
var bolde = "";
for (var k = 0; k < key; k++)
{
bolde += "" + array[i][k] + "";
}
for (var l = key; l < array[i].length; l++)
{
bolde += array[i][l];
}
id++;
data += "<div id='" + id + "'>" + bolde + "</div>";
}
}
$('.suggestion').html(data);
$('.suggestion').css({ display: "block" });
if (e.keyCode == 38 || e.keyCode == 40)
{
selectit(e);
}
if (data == "")
{
$('.suggestion').css({ display: "none" });
}
}
}
function hide()
{
$('#suggestion').css({ display: "none" }); ;
}
function selectit(e)
{
var child = document.getElementById("suggestion").childNodes;

for (var i = 0; i < child.length; i++)
{

if (child[i].id == "1")
{
child[i].style.color = "red"; //here is the problem in the code
}
}
}
</script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900