best way to change the elemnt on which the event handler is applied .
hey guys check out the below code ::
- var i = 1;
-
- var funk = function() {
- return i < 5 ? ++i : i = 1 ;
- }
-
- changeHanderler = function(){
- str = funk();
- console.log(str);
- var elem = $('ul li:nth-child('+ i +')');
- }
-
- $('document').ready(function(){
- elem = $('ul li:nth-child('+ i +')');
-
- elem.on('click.ga' , function(){ // even after elem increaments in changeHandler() , elem will still be attached to the old elem :((
- changeHanderler();
- console.log('logged');
- });
-
- });
see the event handler is attached to elem and onclick of elem the function changeHandler is called and inside changeHandler the value of elem is increamented .
now wht i want to happen is i want the event handler to be attached to the new elem but the event handler is still attached to the old elem , even though the value of elem has actually changed .
so my question is whats the best way to change the event handle to be attched to the new elem .