0

I use jQuery autocomplete and have multiple input fields with different IDs, and they are populated from a MySQL database.

  $("#CCU1").autocomplete({
  minLength : 1,
  source: function( request, response ) {
    $.ajax({
      url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
      dataType: 'json',
      data: { 
        term  : $("#CCU1").val(),
        column: 'course',
        tbl   : 'tbl_courses'
      },
      success: function(data){
        if(data.response == 'true') {
          response(data.message);
        }
      }
    });
  }
});

The input fields has IDs of CCU1...CCU5, name='course'. Any idea how to autocomplete the five input fields instead of hardcoding each one?

Course1: <input type='text' name='course[]' id='CCU1'/><br />
Course2: <input type='text' name='course[]' id='CCU2'/><br />
Course3: <input type='text' name='course[]' id='CCU3'/><br />
Course4: <input type='text' name='course[]' id='CCU4'/><br />
Course5: <input type='text' name='course[]' id='CCU5'/><br />
1
  • A pattern you can use: add a dummyclass to each element like .autocompleteHandler and refer to the class as the selector $('.autocompleteHandler').autocomplete(), and use the reference to $(this) inside the function. Commented Dec 23, 2012 at 18:11

2 Answers 2

4

Assuming that your above code is working for one of them, you could do:

$("[id^=CCU]").each(function(){
    $(this).autocomplete({
      minLength : 1,
      source: function( request, response ) {
        $.ajax({
          url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
          dataType: 'json',
          data: { 
            term  : $(this).val(),
            column: 'course',
            tbl   : 'tbl_courses'
          },
          success: function(data){
            if(data.response == 'true') {
              response(data.message);
            }
          }
        });
      }
    });
});
​
Sign up to request clarification or add additional context in comments.

Comments

1

Use $(this) instead of hardcoding the ID:

term: $("#CCU1").val(),

Replace it with:

term: $(this).val(),

Full code:

$("[id^=CCU]").each(function(){
    $(this).autocomplete({
      minLength : 1,
      source: function( request, response ) {
        $.ajax({
          url:"<?php echo site_url().'gsimc/autocomplete'; ?>",
          dataType: 'json',
          data: { 
            term  : $(this).val(),
            column: 'course',
            tbl   : 'tbl_courses'
          },
          success: function(data){
            if(data.response == 'true') {
              response(data.message);
            }
          }
        });
      }
    });
});

7 Comments

I get an error of: Uncaught TypeError: Cannot call method 'toLowerCase' of undefined
That toLowerCase function is not used in the above script. Can you show us the full code?
The error is in jquery.min.js, I have no problem showing the autocomplete for just one input field though.
I really can't figure this one out. I tried everything all of you have suggested and still getting the uncaught type error thingy. Maybe I need to sleep this out first. :(
This worked for me to get the id of the element being used: $(this)[0].element.attr('id')
|

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.