0

sorry to post what i'm sure is a simple questin but I am trying to get data from a database and then based on a simple searh fill in a form. On my quest for this I found this example. JSFiddle.

This seems to be more or less what I want if I expand to my database size. But for the life of me I cannot even get that script working before I have tried adapting it! I always have trouble with getting javascript and Jquery to work so thoigh I woul dask incase its something stupid i'm doing.

So I have placed jquery 1.7.2 and 1.8.16 UI in within my headlike so

<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.21/themes/black-tie/jquery-ui.css" type="text/css" media="screen" />

and then placed this code in my body

  <script>
    var src = [{
        "label": "Jack",
            "value": "1"
    }, {
        "label": "back",
            "value": "2"
    }, {
        "label": "tera",
            "value": "3"
    }, {
        "label": "judo",
            "value": "4"
    }];
     $('.name').change(function() {
     $('.value').val(0);
    });

    $(".name").autocomplete({
        source: src,
        select: function (event, ui) {
            event.preventDefault();
            this.value = ui.item.label;
            $('.value').val(ui.item.value);
        }
    });
  </script>
  <input type="text" class="name" />
  <input type="text" class="value" />

Thats it! Exactly the same as the JSfiddel example and yet it does not work. Any ideas?

Like I said, I seem to always have issue with javascript and Jquery codes and would bery much like to get to the bottom of what I do wrong.

Any help appreciated.

Ian

snookian
  • 863
  • 6
  • 13
  • 35
  • have you tried clearing cache? – treyBake Jul 06 '17 at 11:45
  • The elements don't exist at the time you run the code that looks for them. – charlietfl Jul 06 '17 at 11:46
  • the given code works for me (when i replace jQuery file with this version `` and wrap script code inside `$(document).ready(function(){...});`) – Alive to die - Anant Jul 06 '17 at 11:48
  • @Alive to Die Thanks. It seems to be doing somethign now I have wrapped it in a function. Did't need to replace the jQuery version. Is it standard to ALWAYS have somwething wrapped in a function? – snookian Jul 06 '17 at 11:51
  • https://jsfiddle.net/ms8yj2df/ Your code will work when you wrap your script code inside`$(document).ready(function(){..});` Cases:- 1.if you are adding script at the bottom of the page (at the end) then this above not needed. 2. if you are adding script code on top or middle in body then this wrapping required. – Alive to die - Anant Jul 06 '17 at 11:55
  • Thanks. I will mark your answer as correct if you want to add it. – snookian Jul 06 '17 at 12:01

0 Answers0