Creating an autocomplete field within a dialog

Creating an autocomplete field within a dialog

Greetings all,
 
Currently I'm working on creating a dialog window that has an autocomplete field within it. I've done autocomplete fields before, along with dialog windows, but never the two together and am finding an issue. Currently, this is what my code looks like.
 
View (parent)
  1. ...
  2. <div id="divPOExistingProduct">
  3. </div>
  4.  
  5.  $("#divPOExistingProduct").dialog({
  6. height: 500,
  7. width: 700,
  8. modal: true,
  9. autoOpen: false,
  10. draggable: false,
  11. resizable: false,
  12. open: function () { },
  13. close: function () { $("#divPOExistingProduct").html(""); }
  14. });
  15.  
  16. //"Add Existing" button
  17. $("#addexisting-poproduct").click(function (e) {
  18. e.preventDefault();
  19. var link = '@(Url.Action("POExistingProductAddPopup", "PurchaseOrder", new { purchaseOrderId = Model.Id }))';
  20. existingProductDialog.load(link);
  21. existingProductDialog.dialog("open");
  22. return false; });
  23. ...

View (dialog)

  1. ...
  2. <tr>
  3. <td class="adminTitle">
  4. @Html.NopLabelFor(model => model.Name):
  5. </td>
  6. <td class="adminData">
  7. @Html.EditorFor(model => model.Name)
  8. @Html.ValidationMessageFor(model => model.Name)
  9. </td>
  10. </tr>
  11. ...
  12.  
  13. $("#Name").keypress(function () {
  14. var productName = $("#Name").val(); //0 based length, so add 1
  15. if (productName.length + 1 > 2)
  16. {
  17. //Do autocomplete functionality
  18. $("#Name").autocomplete({ //Here is where issue starts
  19. source: function (request, response) {
  20. //Get available Products based on search parameter and map data
  21. $.getJSON('@Url.Action("GetProducts", "PurchaseOrder")', { searchProduct: productName }, function (data) { response($.map(data, function (item) {
  22. return { value: item.Name, id: item.Id };
  23. }));
  24. })
  25. },
  26. //Used to dictate actions when user selects an option within autocomplete
  27. select: function (event, ui) {
  28. //Check to see if user tried to select the "No results" option
  29. if (ui.item.id == 0) {
  30. //Do some house cleaning and alert user to mistake
  31. alert("You must retry your search for a Product");
  32. $("#Name").val("");
  33. $("#ProductId").val("");
  34. return false;
  35. }
  36. //Record ProductId
  37. $("#ProductId").val(ui.item.id);
  38. }
  39. });
  40. }
  41. });
 
The problem begins when the autocomplete method itself is created. I get the following error:
 
Error: Object doesn't support this property or method.
 
The error points to where I make my declaration on the autocomplete functionality (within the if loop for the keypress). Originally I thought Name just wasn't being found but several tests showed that it exists. I then thought maybe it had something to do with the masterpage this view inherits, which may or may not. It includes the css for jquery-ui a second time (since dialog is part of original View technically) but even removing the second version of it does nothing.
 
Any suggestions would be appreciative.
 
Kindest Regards,
Chad Johnson