1

I am using JQuery autocomplete option to get values from database using PHP. I have 2 text boxes and I am trying to use same PHP file to get the required results. My HTML code is

<tr>
 <td>LOB</td>
  <td><input autocomplete="off" type="text" name="ABC" class="autocomplete"/></td>
</tr>
<tr>
 <td>Project Name</td>
 <td><input autocomplete="off" type="text" name="PQR" class="autocomplete"/></td>
</tr>

and my JQuery is

$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $(".autocomplete").attr('name') + '&',
minLength: 2
});
});

But as these there are 2 elements with same class. I am not getting correct arrr('name'). MySQL will differ based on this name. I tried (this) but still not getting the result. If I am in the first text box then name should be of that text box etc.. Please let me know if I am missing anything.

1
  • u want value from txtbox1 ,or txtbox2? Commented Apr 19, 2014 at 7:40

3 Answers 3

1

You can target the textbox using name.

$('[name=ABC]').val()
Sign up to request clarification or add additional context in comments.

Comments

0

Use id to differentiate

<tr>
 <td>LOB</td>
  <td><input autocomplete="off" type="text" name="ABC" id="id1" class="autocomplete"/></td>
</tr>
<tr>
 <td>Project Name</td>
 <td><input autocomplete="off" type="text" name="PQR" id="id2" class="autocomplete"/></td>
</tr>

//Script

$(document).ready(function() {
$( ".autocomplete" ).autocomplete({
source: 'LiveSearch.php?name='+ $("#id1").attr('name') + '&',
minLength: 2
});
});

1 Comment

Hi Pratik, I am using same LiveSearch.php where I have defined a logic to read attribute name and fetch the respective data. each(function) is working. Thanks a lot for your reply.
0

You're mixing the scope of 'all autocompletes' and 'the current automplete'. One way of dealing with this is to use jQuery's each() method:

$('.various-things').each(function () {
  console.log($(this).attr('something');
});

This would log the 'something' attribute of each .various-things on the page. It looks like what you'd need is:

$(document).ready(function() {
  $( ".autocomplete" ).each(function () {
    $(this).autocomplete({
      source: 'LiveSearch.php?name='+ $(this).attr('name') + '&',
      minLength: 2
    });
  });
});

Now it'll find all autocompletes and set them up using their own name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.