Search code examples
jqueryunused-variables

JQuery Unused Variable Error


I have a script to populate data from database but the variable i'm trying to use variable selected seems to not being used. What I mean by that is Netbeans is telling me that the variable is unused. Is there something wrong with the script?

function get_child_options(selected)
{
    if (typeof selected === 'undefined')
    {
        var selected = ' ';
    }

    var parentID = jQuery('#parent').val();

    jQuery.ajax(
    {
        url: '/MyProjectName/admin/parsers/child_categories.php',
        type: 'POST',
        data:
        {
            parentID: parentID,
            selected: selected
        },
        success: function(data)
        {
            jQuery('#child').html(data);
        },
        error: function()
        {
            alert("Something went wrong with the child options.")
        },
    });
}

jQuery('select[name="parent"]').change(get_child_options);

Solution

  • Remove your selected variable. You have a function parameter and a variable with the same name.

    function get_child_options(selected)
    { 
         if(typeof selected === 'undefined')
         {
             selected = ' ';
         }
    
         var parentID = jQuery('#parent').val();
         jQuery.ajax(
         {
             url: '/MyProjectName/admin/parsers/child_categories.php',
             type: 'POST', 
             data: {'parentID': parentID, 'selected': selected}, 
             success: function(data)
             { 
                 jQuery('#child').html(data); 
             },
             error: function()
             {
                 alert("Something went wrong with the child options.")
             }
         }); 
    
         jQuery('select[name="parent"]').change(get_child_options);
    }