I have a module for relating pieces of content to each other. My module successfully adds a field to all content-types that is essentially a list of integers, meant to hold node ID's. I want AJAX functionality such that when the user begins typing the title of a node, autocomplete suggestions begin appearing. I've already implemented the web service via Drupal to retrieve nodes based on partial titles, and that's working very well. The problem is now attaching the behavior I want to the form element.
Two problems:
1) No way to uniquely identify the form element in question. The class name is not going to be usable if there are more than one of them, which there may be. How can I force a unique class name? Something like class="content-relation" for each one?
2) The behavior I want to attach is failing to do so. I've tried a couple different ways...here they all are.
Way 1:
Drupal.behaviors.content_relation = {
attach: function(context) {
jQuery('input.form-text').change(function() {
var ul = jQuery('<ul />')
.css({position: 'absolute', top: '0px', left: '0px'});
jQuery.get(
'/api/'+jQuery(this).val(),
function (data) {
jQuery.each(data, function(item){
var li = jQuery('<li>');
li.html(item.title);
li.appendTo(ul);
});
}
);
});
}
};
Way 2:
jQuery.ready(function(){
jQuery('input.form-text').live('change', function() {
var ul = jQuery('<ul />')
.css({position: 'absolute', top: '0px', left: '0px'});
jQuery.get(
'/api/'+jQuery(this).val(),
function (data) {
jQuery.each(data, function(item){
var li = jQuery('<li>');
li.html(item.title);
li.appendTo(ul);
});
}
);
});
});
...but none of the inputs with a class of form-text have this function listed in their change-listeners, nor do they respond to changes. Thoughts?
document.write
orappend
theul
var somewhere? Looking at your code as it stands, it gets populated with <li>'s but never gets printed or appended anywhere. Also it sounds like you're recreating the References module.