Zoha Ali Khan
Zoha Ali Khan

Reputation: 1699

function statement requires a name error in JQuery

I have this function but it gives me function statement require name onStop function

<script type="text/javascript">
 jQuery( function($) {
    $('#Hnav, #Nnav').NestedSortable(
            {
                accept: 'sort',
                noNestingClass: "no-children",
                helperclass: 'helper',
                autoScroll: true,
                onChange: function(serialized) {
                onStop : function(){
                    $('#output').html($(this).id);
                },
                    nestingPxSpace : '0'
            }
    );
});
</script>

Upvotes: 1

Views: 2681

Answers (3)

Adil
Adil

Reputation: 148110

There was a bracket missing and you use wrong syntax getting id try this,

$('#output').html($(this).id);

should be

$('#output').html(this.id); 

or

$('#output').html($(this).attr(id));

  jQuery(function($) {
    $('#Hnav, #Nnav').NestedSortable({
        accept: 'sort',
        noNestingClass: "no-children",
        helperclass: 'helper',
        autoScroll: true,
        onChange: function(serialized) {
            alert("changed");// Changed to fix
        },               
        onStop: function() {
             $('#output').html(this.id);
        },
        nestingPxSpace: '0'       
    });
});​

Upvotes: 1

Umesh Aawte
Umesh Aawte

Reputation: 4690

Collectively there are two problems in the code,

  1. The inner function was not closed properly.
  2. Used id property of element that is not supported by the jQuery selector.

The modified code is,

jQuery( function($) {
    $('#Hnav, #Nnav').NestedSortable(
            {
                accept: 'sort',
                noNestingClass: "no-children",
                helperclass: 'helper',
                autoScroll: true,
                onChange: function(serialized) {
                    //A empty function
                },
                onStop : function(){
                    $('#output').html($(this).attr("id"));
                },
                nestingPxSpace : '0'
            }
    );
});

Upvotes: 1

VisioN
VisioN

Reputation: 145398

In this context, this refers to the DOM element on which the onStop event happens. A DOM element is not a jQuery object.

jQuery $(this) object doesn't have id property, while the DOM elements do. So, use either:

$('#output').html(this.id);

or:

$('#output').html($(this).attr("id"));

And don't forget to close the bracket in onChange handler function.

Upvotes: 4

Related Questions