javascript - How to invoke a JS function from several controls -


i have several controls in table, ids , functions generated in each row of table this:

<select id="data_01" onclick="open_details_01()">... <select id="data_02" onclick="open_details_02()">... <select id="data_03" onclick="open_details_03()">... 

i need 1 function like:

function open_details_ + *index* () { } 

when name of function starting "data_"

you can 1 of 2 ways:

  1. use eval: `eval( functionname + num + "();" )
  2. you can make function part of object , call member: myobject[ functionname + num ]()

to second one:

    var obj = {       open_details_01: function()       {         alert( "1" );       },       open_details_02: function()       {         alert( "2" );       },       open_details_03: function()       {         alert( "3" );       }     };      var num = "01";     obj[ "open_details_" + num ]();     num = "02";     obj[ "open_details_" + num ]();     num = "03";     obj[ "open_details_" + num ](); 

fiddle of second way: https://jsfiddle.net/69lk5baa/


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -