Phill Healey
Phill Healey

Reputation: 3180

JQuery Method is not defined

I have the following JS/JQuery in the body of a my JQM (html) site, when I try to call sortMethod() I get 'ReferenceError: sortMethod is not defined' in FireBug console.

<script>
        $(function(){
            $("[name=radio-choice-h-3]").change(function() {
                //alert('Selected: '+$('input[name=radio-choice-h-3]:checked').val());
                sessionStorage.sortBy = $('input[name=radio-choice-h-3]:checked').val();
                sortMethod();
            });
        });
        $(function(){
            sortMethod();
        });      
        $(function sortMethod(){       
            if(sessionStorage.sortBy === "model" || sessionStorage.sortBy == null || sessionStorage.sortBy.trim())
            {
                theManufacturers('model');
                $(document).on("pagecontainerbeforeshow", function () {
                    var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                    $("#radio-choice-h-2a", page).prop("checked", true).checkboxradio("refresh");
                });
            }
            else if(sessionStorage.sortBy === "year")
            {
                theManufacturers('year');
                $(document).on("pagecontainerbeforeshow", function () {
                    var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                    $("#radio-choice-h-2b", page).prop("checked", true).checkboxradio("refresh");
                });
            }
            else if(sessionStorage.sortBy === "location")
            {
                theManufacturers('location');
                $(document).on("pagecontainerbeforeshow", function () {
                    var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                    $("#radio-choice-h-2c", page).prop("checked", true).checkboxradio("refresh");
                });
            }
            else if(sessionStorage.sortBy === 'ttaf')
            {
                theManufacturers("ttaf");
                $(document).on("pagecontainerbeforeshow", function () {
                    var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                    $("#radio-choice-h-2d", page).prop("checked", true).checkboxradio("refresh");
                });
            }
        });

        $(function theManufacturers(inputSearch){
            var qryString = 0;

            //set up string for adding <li/>
            var li = "";
            var jqxhr = $.getJSON("url",
            function(data){
                    $.each(data.items, function(i,item){
                        li+='<li><a href="#" data-transition="slidedown"><img src="' + item.Image.trim() + '" style="height:80px;/> <span style="font-size:0.75em;">' + item.Manufacturer.trim() + ' ' + item.Model.trim() + '(' + item.Price.trim() + ')</span><br/><span style="font-size:0.65em;">S/N: ' + item.Serial.trim() + ' | TTAF: ' + item.TTAF.trim() + ' | LOC: ' + item.Location.trim() + '</span><br/><span style="font-size:0.65em;">' + item.DealerName.trim() + '</span></a></li>';
                    });

                    $("#results-list").append(li);
                    $("#results-list").listview("refresh");
                });
            //jqxhr.done(function() {
             //   console.log( "second success" );
            //});
        });

What am I doing wrong?

Upvotes: 0

Views: 124

Answers (2)

john Smith
john Smith

Reputation: 17906

<script>
            //first you define your functions, they will not be executed "from alone"
  //syntax:|function name() { .. code }
            function sortMethod(){       
                if(sessionStorage.sortBy === "model" || sessionStorage.sortBy == null || sessionStorage.sortBy.trim())
                {
                    theManufacturers('model');
                    $(document).on("pagecontainerbeforeshow", function () {
                        var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                        $("#radio-choice-h-2a", page).prop("checked", true).checkboxradio("refresh");
                    });
                }
                else if(sessionStorage.sortBy === "year")
                {
                    theManufacturers('year');
                    $(document).on("pagecontainerbeforeshow", function () {
                        var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                        $("#radio-choice-h-2b", page).prop("checked", true).checkboxradio("refresh");
                    });
                }
                else if(sessionStorage.sortBy === "location")
                {
                    theManufacturers('location');
                    $(document).on("pagecontainerbeforeshow", function () {
                        var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                        $("#radio-choice-h-2c", page).prop("checked", true).checkboxradio("refresh");
                    });
                }
                else if(sessionStorage.sortBy === 'ttaf')
                {
                    theManufacturers("ttaf");
                    $(document).on("pagecontainerbeforeshow", function () {
                        var page = $.mobile.pageContainer.pagecontainer("getActivePage");
                        $("#radio-choice-h-2d", page).prop("checked", true).checkboxradio("refresh");
                    });
                }
            };


            //same here, just a defined function
            function theManufacturers(inputSearch){
                var qryString = 0;


                var li = "";
                var jqxhr = $.getJSON("url",
                function(data){
                        $.each(data.items, function(i,item){
                            li+='<li><a href="#" data-transition="slidedown"><img src="' + item.Image.trim() + '" style="height:80px;/> <span style="font-size:0.75em;">' + item.Manufacturer.trim() + ' ' + item.Model.trim() + '(' + item.Price.trim() + ')</span><br/><span style="font-size:0.65em;">S/N: ' + item.Serial.trim() + ' | TTAF: ' + item.TTAF.trim() + ' | LOC: ' + item.Location.trim() + '</span><br/><span style="font-size:0.65em;">' + item.DealerName.trim() + '</span></a></li>';
                        });

                        $("#results-list").append(li);
                        $("#results-list").listview("refresh");
                    });

            }); 


            //here is an example how i outsourced your changehandler to a function
            function initializeChangeHandler(){
                $("[name=radio-choice-h-3]").change(function() {
                    //alert('Selected: '+$('input[name=radio-choice-h-3]:checked').val());
                    sessionStorage.sortBy = $('input[name=radio-choice-h-3]:checked').val();
                });
            };


            //heres the function that will be executed when the document model is complete loaded
  //syntax:|$(function(){ .. code .. })
            $(function(){ // as the code inside here is executed, you can now call your functions
                    initializeChangeHandler();// <<-- example for understanding
                    sortMethod(); // <<-- heres the call
            });


</script>

keep up to this structure ( initialize the stuff you defined before) then you can be sure your functions are defined ;9

Upvotes: 1

Hadi Sharifi
Hadi Sharifi

Reputation: 1527

Move sortMethod in outer of scope $(), I mean sortMethod not need $()

function sortMethod(){  
....
}

Upvotes: 0

Related Questions