0

I have a website with Bootstrap framework. I have two very nasty input fields I'm stuck with, because I can't get them to work properly...

One is this Bootstrap Typeahead input field:

<input type="text" id="typeahead" name='name' placeholder='The name' class="form-control" data-provide="typeahead" autocomplete="off" />

The other is this dropdown list:

<div class="btn-group">
            <button class="btn btn-default dropdown-toggle" id="dates-dropdown-button" type="button" data-toggle="dropdown" href="#">
                <?=$reminder_table_th_date?>
                <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="name-dates-dropdown">
              </ul>
</div>

(the <li> list is populated via Ajax call)

With both I want to do: if I select one item either on the ajax dropdown field with the typeahead or with the dropdown list the selected item should be the value what the field is showing. I tried many-many ways, for example:

How to Display Selected Item in Bootstrap Button Dropdown Title

Bootstrap 3 dropdown select

Bootstrap typeahead - How to set the default value manually

but none of them is working and I don't know why. I'm not a JS/jQuery guru at all, but that's just suspicious. Where exactly should I place the jQuery code for this functions and how?

PS: Firefox Firebug isn't showing any JS errors, they just don't do anything, the values aren't set OR the function for them isn't called.

This is the jQuery code for the typeahead Ajax call, the dropdown population (this is working well) and there is also one line where the typeahead value should be set (in the updater), but sadly that's not working:

<script type="text/javascript">

    $(document).ready(function() {
      $('#typeahead').typeahead({
        source: function (query, process) {
          $.ajax({
            url: '/functions/name-autocomplete.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + query,
            success: function(data) {
              process(data);
            }
          });
        },
        displayText: function(item) {
            return item
        },
        updater: function (item) {
            $('#typeahead').typeahead('val', item);
            $.ajax({
            url: '/functions/name-dates-autocomplete.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + item,
            success: function(data) {
              $('#name-dates-dropdown').html(data);
            }
          });
        }
      });
    });

</script>
Community
  • 1
  • 1
VORiAND
  • 145
  • 3
  • 17
  • 35
  • *the selected item should be the value what the field is showing* could you elaborate a bit more on this ? – Searching Jan 10 '17 at 21:43
  • Sure. The input text field should set this as VALUE, the dropdown list as selected item, so replace the value what is right now one PHP variable: =$reminder_table_th_date?> – VORiAND Jan 10 '17 at 22:03
  • In order to set the
  • element as selected we need to identify the element. A sample of the `
  • ` returned by would be useful
  • – Searching Jan 10 '17 at 22:14
  • but how is this associated with the typeahead text or the `=$reminder_table_th_date?>` ? How can i say that "February 27" is what i want to mark as selected ? – Searching Jan 10 '17 at 22:43
  • "How can i say that "February 27" is what i want to mark as selected " because you click on it... If I click on this element, then this is the SELECTED one. I have two separated field. One is the ajax dropdown on the typeahead and the other is the dropdown list. I think you misunderstood something. – VORiAND Jan 11 '17 at 12:07