jquery - Is it possible to use $(this) in a function called from another function? -


sorry question title, know it's not worded, couldn't explain i'm asking in summary.

i have jquery function tied click event of element. reasons may flawed (other making more sense me personally), within function calling function. second function, i'd able call $(this) first function, i'm not sure if it's possible, or how if can

example

jquery function works

$('.member-div a').on("click",function(f){     f.preventdefault();     var x = $(this).parent('div').attr('id').slice(-1);     $('.member-div').hide();     if($(this).hasclass('next')){         var y = parseint(x)+1;     };     if($(this).hasclass('back')){         var y = parseint(x)-1;     };     $('#section'+y).show(); }); 


what i'm trying do

$('.member-div a').on("click",function(f){     f.preventdefault();     newfunc(); });  function newfunc(){     var x = $(this).parent('div').attr('id').slice(-1);     $('.member-div').hide();     if($(this).hasclass('next')){         var y = parseint(x)+1;     };     if($(this).hasclass('back')){         var y = parseint(x)-1;     };     $('#section'+y).show(); }; 

i think understand why second way doesn't work ($(this) not anchor tag initial function), lost how make work

if have to, or if can explain that/why better to, stick original example works!

thanks

always careful when using this may end using unexpected value depending on context function called.

instead, should consider using parameter:

$('.member-div a').on("click",function(f){     f.preventdefault();     newfunc($(this)); });  function newfunc(item){     var x = item.parent('div').attr('id').slice(-1);     $('.member-div').hide();     if(item.hasclass('next')){         var y = parseint(x)+1;     };     if(item.hasclass('back')){         var y = parseint(x)-1;     };     $('#section'+y).show(); }; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -