Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/08/2015, 13:07
pablofa
 
Fecha de Ingreso: febrero-2015
Ubicación: Murcia
Mensajes: 29
Antigüedad: 9 años, 2 meses
Puntos: 0
Obtener innerHTML del div que lanza la función con onclick

Buenas, tengo un input type text en el que cuando empiezas a escribir, se despliega una lista de sugerencias. Todo funciona bien hasta que llega el momento de seleccionar el elemento deseado, que no ocurre nada. He creado una funcion con javascript que cuando se produce el evento onclick sobre el div que contiene el hotel que quiero seleccionar, en teoría debería escribírmelo en el input type text, pero no lo hace y no se donde está el error...
Me podrían ayudar????

Les dejo el código html, javascript y php.

Muchas gracias.



Código HTML:
<input class="campoFormulario2" type="text" id="hotel" name="hotel" placeholder="Escribe tu hotel" onkeyup="llenarListaHoteles()" onBlur="vaciarLista()" />
<div class="sugerencias" id="lista"></div>




function nuevoAjax()
{ 
	var xmlhttp=false;
	try
	{
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}

function llenarListaHoteles(){
	
	var eleccionProvincia = document.getElementById("provincia").value;
	var ajax=nuevoAjax();
	var pistaEscrita = document.getElementById("hotel").value;
	
	ajax.open("GET", "sugerencias.php?provincia="+eleccionProvincia+"&pista="+pistaEscrita, true);
	ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==4){
				document.getElementById("lista").innerHTML=ajax.responseText;
			} 
			else{
			}
		}	
	ajax.send(null);	
}


function vaciarLista(){
	document.getElementById("lista").innerHTML='';
}

function elementoSeleccionado(){
	var seleccion = this.innerHTML;
	document.getElementById("hotel").innerHTML = seleccion;
}




Código PHP:
$provincia $_GET["provincia"];
$pista $_GET["pista"];

$consulta mysql_query("SELECT provincia, hotel FROM hoteles WHERE provincia='$provincia' AND hotel LIKE '%$pista%'");
while(
$fila mysql_fetch_array($consulta)){
    echo
'<div onClick="elementoSeleccionado()">'.$fila["hotel"].'</div>';