3
var TourItinerary = {  
    EXCURSIONS:{

    },
    init:function(){
        console.log('Initializing...');
        this.initializeElements();
    },       
    initializeElements:function(){
        console.log('init elements...');
        $("select[id^='start_city_']").change(function(){
            var day_id = $(this).attr('id').replace("start_city_","");
            getPossibleExcursions(day_id);
        });
    },    
    getPossibleExcursions: function(day_id){
        console.log('Day Id ='+day_id);
    }
};

how to call getPossibleExcursions(day_id) function inside dropdown change event?

It always shows:

Uncaught ReferenceError: getPossibleExcursions is not defined.

5
  • var self = this; at the start of the function, then self.getPossibleExcursions(day_id); Commented Mar 6, 2018 at 9:31
  • Thanks for the reply. Devesh now it shows Uncaught RangeError: Maximum call stack size exceeded Commented Mar 6, 2018 at 9:37
  • TourItinerary.getPossibleExcursions if you only need this to work “statically” ... Commented Mar 6, 2018 at 9:41
  • CBore thanks for the replay. But still get the same error. Uncaught RangeError: Maximum call stack size exceeded Commented Mar 6, 2018 at 9:43
  • Use self.getPossibleExcursions(day_id); Commented Mar 6, 2018 at 9:46

1 Answer 1

1

You have to assign this to some variables for future reference. When you define change event, this refers the DOM object that you are going to add event, not the actual object. So assign this to some variables, say self and reuse it in the on change function.

Now you can use both self for referring the object and this for the DOM object

var TourItinerary = {  
    EXCURSIONS:{

    },
    init:function(){
        console.log('Initializing...');
        this.initializeElements();
    },       
    initializeElements:function(){
        console.log('init elements...');
        var self = this;
        $("select[id^='start_city_']").change(function(){
            var day_id = $(this).attr('id').replace("start_city_","");
            self.getPossibleExcursions(day_id);
            console.log("value = " + $(this).val());
        });
    },
    getPossibleExcursions: function(day_id){
        console.log('Day Id ='+day_id);
    }
};

$(document).ready(function(){
    TourItinerary.init();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="start_city_123">
   <option value="a">a</option>
   <option value="b">b</option>
   <option value="c">c</option>
   <option value="d">d</option>
   <option value="e">e</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.