javascript - js function for multiple variables -


so, have 2 js variables use lot:

var rhpp = jquery(this).parents('.rhp'); var rhp_id = rhpp.data("pi"); 

for example:

function my_function_1 (){    jquery(document).on('click', '.button_1', function (e) {       var rhpp_1= jquery(this).parents('.rhp');       var rhp_id_1 = rhpp_1.data("pi");          //do    }); } function my_function_2 (){    jquery(document).on('click', '.button_2', function (e) {       var rhpp_2 = jquery(this).parents('.rhp');       var rhp_id_2 = rhpp_2.data("pi");          //do    }); } function my_function_3 (){    jquery(document).on('click', '.button_3', function (e) {       var rhpp_3 = jquery(this).parents('.rhp');       var rhp_id_3 = rhpp_3.data("pi");          //do    }); } 

because of it, want make function can reuse:

function rhp_parent(a, b) {     var = jquery(this).parents('.rhp');     var b = a.data("pi"); } 

then, rhp_parent("rhpp", "rhp_id");

of course, not right. not familiar how make function variables.

could show me?

thanks!

you create function returns both of values.

function getrhpparent(element) {   var rhpp = jquery(element).parents('.rhp');   return {     rhpp: rhpp,     rhpp_id: rhpp.data("pi")   }; }  // usage var temp = getrhpparent(this); var rhpp = temp.rhpp; var rhp_id = temp.rhp_id; 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -