Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently developing a plugin for wordpress, and using jquery autocomplete. In my plugin file i enque autocomplete like this:

wp_enqueue_script('jquery-ui-autocomplete');

Then i assign the script to an input id like this:

jQuery("#post_email_repeatable").autocomplete({
      source:"get_posts.php",
      minLength:1
})

This works fine for one input. However i am using repeatable / reuasble input fields (they can be created on the fly). They all have the same id of #post_email_repeatable.

Auto-complete will only work on the first input with the id of #post_email_repeatable.

My question is, is it possible to get auto-complete to work on work than one input with the same id?

share|improve this question
2  
ID's must be unique, this is not specific to WordPress. Use a class. –  Milo Jan 3 at 15:18

1 Answer 1

Auto complete is most likely not working on the extra fields because they are added with javascript dynamically, and when you make your javascript .autocomplete() call, it runs on page load. Your dynamically created fields do not exist yet to have autocomplete hooked with them. Try this.

jQuery(document).on("keydown", ".post_email_repeatable", function(){
    jQuery(this).autocomplete({
        source: "get_posts.php",
        minLength: 1
    });
});

This binds the autocomplete function the input field with that class whenever the keydown event happens on it. So it will trigger even when keydown event happens on newly dynamically created fields.

Also as Milo commented, you should use a class as ID's should be unique, however not having unique IDs shouldnt break the functionality of it. I still suggest you add a "post_email_repeatable" class to each repeatable field.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.