jquery autocomplete with dialog updating value
Asked Answered
P

1

0

I have a form that opens up in a dialog. One of the fields has autocomplete. All fields are built and server values are stored in them to pre-fill the form.

var mydiv = jQuery("#editform");
var $myform = jQuery("<form id='EditForm' method='post' action='index.php?option=com_component&task=edit'></form>");
...
var $mylabel10 = jQuery("<label for='EditSelect'>A label</label>");
var $myinput9 = jQuery("<input id='EditSelect' name='EditSelect' type='text' />");
var $mylabel9 = jQuery("<label for='EditSelect2'>Another label</label>");
var $myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + "' />"); //path is a value passed in from the server

$myform.append(... $mylabel10, $myinput9, $mylabel9, $myinput8);
mydiv.append($myform);

    //autocomplete code - order is important to have autocomplete go outside dialog
    var available = [
             { label : 'foo', value : 'bar' },
             { label : 'xyz', value : 'abc' },
             ...
         ];
    jQuery( "#EditSelect", mydiv ).autocomplete({
        source: available,
        focus : function(){ return false; }
    })
    .on( 'autocompleteselect', function( e, ui ){
        var t = jQuery(this),
          details = jQuery('#EditSelect2'),
          label = ( e.type == 'autocompleteresponse' ? ui.content[0].label :  ui.item.label ),
          value = ( e.type == 'autocompleteresponse' ? ui.content[0].value : ui.item.value );

      t.val( label );
      details.val( value ); //doesn't update the form here?

      return false;
    });

     // get reference to autocomplete element
    var autoComplete = jQuery("#EditSelect", mydiv).autocomplete("widget");

    var dialogOpts = {
            modal: true,
            autoOpen: true,
            resizable: false,
            width: 525,
            height: 'auto',
            title: 'Edit settings'
        };

    mydiv.dialog(dialogOpts);  
    autoComplete.insertAfter(mydiv.parent());

In this edit dialog is an autocomplete that when selected it should update the other input field (#EditSelect2). Currently #EditSelect2 has the value from the server (in the variable path).

When a new value is selected from the autocomplete I would expect the form updated because of this code: details.val( value );. Right now the autocomplete works fine but the value from the server (path) doesn't get updated after selecting a new choice in the autocomplete.

Hope this makes sense.

Pentobarbital answered 21/6, 2013 at 19:30 Comment(0)
F
1

There is a small syntax error in your myinput8 statement:

$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + " />");

should be:

$myinput8 = jQuery("<input id='EditSelect2' name='add_path' value='" +path + "' />");

Note the extra single quote at the end.

Ferree answered 22/6, 2013 at 21:24 Comment(4)
thanks for looking but that was just a cut/paste error with the question when I removed other junk not relevant to the question.Pentobarbital
@Tom: but after this fix this fiddle: jsfiddle.net/3FJuC seems to work (using FF21).Ferree
Thanks for sticking with it and showing me it did work. I had duplicate ids...sigh.Pentobarbital
No problem, glad you found the problem.Ferree

© 2022 - 2024 — McMap. All rights reserved.