auto complete jQuery with 2 input

0

I have the following autocomplete that works on the 1st input, when it completes it fills the 2nd input.

The problem is that if you delete the <p> tag it does not work. Can anyone tell me why?

Follow the code:

$().ready(function() {

  $("#singleBirdRemote").autocomplete("search.php", {
    width: 260,
    selectFirst: false
  });

  $("#singleBirdRemote").result(function(event, data) {
    if (data)
      $(this).parent().next().find("#xx").val(data[0]);
  });

});
<p>
  <input type="text" id="singleBirdRemote">
</p>
<p>
  <input name="asdad" id="xx">
</p>
    
asked by anonymous 18.03.2016 / 12:06

1 answer

2

I think this happens because you retrieve the reference of the second input relative, by traversing the DOM tree.

That is, you are based on #singleBirdRemote , search for the parent p , navigate to the next element, and search for the xx input. When you remove p you break this hierarchy.

 $(this).parent().next().find("#xx").val(data[0]);

You can replace the excerpt simply by

 $("#xx").val(data[0]);

I believe this will only help.

Take care when browsing in a relative way, any changes to the code can cause you problems. The ideal is to have precise references, by id, name or class, depending on the need.

    
18.03.2016 / 14:06