user3526204 user3526204 - 2 months ago 4x
jQuery Question

How to handle variable inside javascript

I am trying to amateurishly create an autocomplete input field with jquery. My first step is to get some results displayed below the input field by calling a php script on keyup with the load function of jquery.

I dont know how silly this attempt is, but i think i am almost there. However, I am stuck during passing the keyup variable to the script. Actually I can passa a variable but i need to use the keyup variable to pass.

For clarity on the question please see the code;

<script src=""></script>
function getres(){
var x = document.getElementById("cit");


<input type="" name="" id="cit" onkeyup="getres()">
<div id='resdiv'></div>

PHP file being called results.php


if(isset($_GET['cit'])){echo $_GET['cit']."<br>";}
$getprops=$clientdb->query("SELECT * FROM cities ORDER BY DATE LIMIT 6");

echo $info['city']."<br>";


I m able to get the results in the div below with id resdiv. But the problem is, how do I ensure the value of Var X in the JavaScript function is appended to the load parameter results.php?cit= ?

I get 'x.value' being passed rather than the actual value of x. I think once I do that, eventually I can match the variable to the results array and show matching auto-complete kind of results.


You need to put x.value outside your quotes as that's a variable. You can make your code much shorter than it currently is. You should not have any issues with following way:

$("#cit").on("keyup", function(){
    var x = document.getElementById("cit").value;

<input type="" name="" id="cit">
<div id='resdiv'></div>