1

apparently my jquery function is not defined. I have no idea why. I am calling jquery before the scripts file so thats not the case and the jquery was working fine before I put it in the function.

(function ($) {

function selectCharacter(){
    $('select.character_select').change(function(){

        alert('Select field value has changed to' + $('select.character_select').val());



    });
}

})(jQuery);


selectCharacter();
Brendonexus
  • 142
  • 1
  • 10
  • 1
    Possible duplicate of [How do JavaScript closures work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Heretic Monkey Apr 07 '17 at 21:38

1 Answers1

5

This is a scope issue. function creates a new scope. So, you're trying to invoke a private variable to your function outside of your function. You can fix this by changing where you invoke the function:

(function ($) {
    function selectCharacter() {
        $('select.character_select').change(function() {
            alert('Select field value has changed to' + $('select.character_select').val());
        });
    }       
    selectCharacter();
})(jQuery);
Derek
  • 4,575
  • 3
  • 22
  • 36
Neil
  • 14,063
  • 3
  • 30
  • 51