views:

47

answers:

1

I'm building an ASP.NET MVC site where I need a tag editor, similar to the one used on Stack Overflow. I've already looked up how to accomplish the necessary autocompletion with jQuery UI, but I've run into a problem: when I place the script in an external .js file, it doesn't execute.

Here is my test.html:

<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Test</title> 
    <script src="http://jqueryui.com/jquery-1.4.2.js"&gt;&lt;/script&gt; 
    <script src="http://jqueryui.com/ui/jquery.ui.core.js"&gt;&lt;/script&gt; 
    <script src="http://jqueryui.com/ui/jquery.ui.widget.js"&gt;&lt;/script&gt; 
    <script src="http://jqueryui.com/ui/jquery.ui.position.js"&gt;&lt;/script&gt; 
    <script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"&gt;&lt;/script&gt;
    <script src="jquery.tagautocomplete.js"></script>
    <script> 
    $(function() { bindAutoTagComplete('#birds'); })
    </script>
</head> 
<body> 
    <label for="birds">Birds: </label> 
    <input id="birds" size="50" /> 
</body> 
</html> 

Here's jquery.tagautocomplete.js:

function bindAutoTagComplete(item, otherRootDomain)
{
        function split( val ) {
            return val.split( / \s*/ );
        }
        function extractLast( term ) {
            return split( term ).pop();
        }

        $(item).autocomplete({
            source: function( request, response ) {
                $.getJSON('http://jqueryui.com/demos/autocomplete/search.php', {
                    term: extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( " " );
                return false;
            }
        });
    }

What do you think may be causing this problem? I'm probably missing some closing parantheses/braces in the .js file...

Thanks in advance!

+1  A: 

You need to attach that event after the page is ready. #birds doesn't exist when it runs currently.

Something like

<script>
$(document).ready( function(){  bindAutoTagComplete('#birds'); } );

</script>
Nik
This is incorrect, `$(function() {` is a shortcut for `$(document).ready(function(){`, so he's already dunning this on `document.ready`.
Nick Craver
That seems to fix the problem. Thanks so much!
Maxim Zaslavsky
@Nick works for me!
Maxim Zaslavsky
@Maxim - You have another underlying issue, or made another change, there is *no* difference in the code posted in the answer here.
Nick Craver
@Nick - where have you seen that shortcut documented?
Nik
@Nik - It's in the API: `$(callback)`: http://api.jquery.com/jQuery/#jQuery3 You'll find it in thousands of SO questions :)
Nick Craver
I agree with @Nick-Craver, this change should have no impact on your code whatsoever.
ArtBIT
Ah cheers - I've been wasting keystrokes! :)
Nik
Hmm, that's true, but I didn't make any other changes! Very strange... oh well, at least it works now!
Maxim Zaslavsky