1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Undefined Variable

Discussion in 'JavaScript' started by Jeremy Benson, Jan 9, 2018.

  1. #1
    I've got a click handler on a button to run a JavaScript pagination feature. For some reason I'm getting error filterSet not defined.

    At the top of my php page:
    echo '<script>
            var search = new Search();
             search.setSearch('. '\'' . urldecode($_GET['search']) . '\''  .');
                               
             $(document).ready(function(){
                                       
                       $( "#loadmore" ).click(function() {
                                  search.fetchSearch();
                         });
                                       
              });
        </script>';
    Code (markup):
    JavaScript Class
    function Search()
    {
        this.ID = 0;
        this.filter = "highest plays";
        this.page = 1;
        this.search = "";
       
        this.fetch = function()
        {
           
            var filterSet = this.filter;
            var pageSet = this.page;
            this.page = this.page + 1;
           
            $.ajax({
                      method: "POST",
                      url: "php/fetch_search_js.php",
                      dataType: 'json',
                      data: { filter: filterSet, pageVal:pageSet}
                    }).done(function( data ) {
                       
                    // the returned data will be the needed ID for pagination
                    // will fetch highest ID from query based on filter
                   
                    if(data.message == "success")
                    {
                       
                        $('#searchresults').append(data.html);
                       
                    }
                       
            });
           
        }
       
        this.fetchSearch = function()
        {
           
            alert("fetch search");
           
        }
       
        this.setFilter(filterSet)
        {
           
            this.filter = filterSet;
           
        }
       
        this.setSearch(searchSet)
        {
           
            this.search = searchSet;
           
        }
    
    // end class       
    }
    Code (markup):

     
    Jeremy Benson, Jan 9, 2018 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I don't see Search.fetch() being called, but if you are doing so from an event handler remember that object.this points at the FUNCTION, not the parent object in those cases. More often than not -- even with the train wreck of developer ineptitude and ignorance that is jQuery -- that is the leading cause of a variable declared on an object not existing in one of that object's methods.

    Pretty much when you try to set "filterSet = this.filter" the FUNCTION doesn't even have a 'filter' property.

    Usually you can get around this by just using locals on the parent object instead of assigning them as properties.
     
    deathshadow, Jan 21, 2018 IP