1

I have this function which is as follows:

        function detectfieldchange(parent = '#slidesContainer', field = '.slide-type-selector') {
            $(parent).on('change', field, function(event) {
                $this = $(this);
                console.log($this);
                fieldtype = '.' + $this.val() + '-options';
                allfieldtypes = $this.closest('.sliderow').find('.slide-type-properties');
                allfieldtypes.fadeOut(300);
                $this.closest('.sliderow').find(fieldtype).fadeIn(300);
            });
        }

and the issue is I want to run the function like this( As follows ) so that I can pass in any selectors I want but this does not work:

detectfieldchange(var1, var2);

Both var1 and var2 are defined variables with CSS selectors in term of strings like this var var1 = '.parent'; etc

While If I simply do a simple call to activate the default values of function params the function works perfectly like this:

detectfieldchange();

So, Having explained this, What could be a possible solution to achieve this. And one thing more to note here is that 'fields' are going to be dynamically added to the page that's why event delegation is necessary.

Zakria B.
  • 31
  • 1
  • 3
  • I would recommend you that you add an Eventlistener that is binded to a fixed class like `.slidesContainer` so everytime you add another Container, it will has the class which was binded and should fire the change event. Look at the "delegated events" part following source: http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements – Cagatay Ulubay Apr 28 '17 at 22:47
  • what is the value of `var1` and `var2` when you invoke the function ? – Sushanth -- Apr 28 '17 at 22:49
  • @Sushanth-- The value of var1 and var2 can be any 2 classes like '.selector1' & '.selector2' respectively. – Zakria B. Apr 28 '17 at 22:52
  • @CagatayUlubay Thank you, Is there any chance or way I can get to change this('slidesContainer') as well? – Zakria B. Apr 28 '17 at 22:54

0 Answers0