Spr243 0 Posted November 7, 2020 Report Share Posted November 7, 2020 How can I run a method stored in a variable? For example, the user selects a method that the user wants it to complete. That method is stuck in the variable, userSelectedMethod. var userSelectedMethod = "toUpperCase()" I want the script to run the method that is in the variable. I tried using the following code. (Didn't work, obviously) /// Attempt 1 var newResult = enteredText.userSelectedMethod; /// Attempt 2 var newResult = enteredText.Module[userSelectedMethod]; /// Attempt 3 var newResult = enteredText.callback(userSelectedMethod); I want the newResult to run whatever method the user Selected Method is. What would be the proper and simplest method of accomplishing this? Note: The variable names and extra steps have been removed and simplified to explain what I'm trying to do. Quote Link to post Share on other sites
JMRKER 10 Posted November 20, 2020 Report Share Posted November 20, 2020 (edited) I not sure why you would want to do this 🤔, but this might meet your needs. [code] <script> var x = 'String to be changed.'; // initialize variable const m = new Map();      m.set('uc',x.toUpperCase());      m.set('lc',x.toLowerCase()); var str = x+'\n'+m.get('uc')+'\n'+m.get('lc'); alert(str); // Change variable contents x = 'Some new string'; alert(x+'\n'+m.get('uc')+'\n'+m.get('lc')); </script> [/code] It uses the Map() function to create objects to manipulate your variable.  BTW, welcome to the forums. Edited November 20, 2020 by JMRKER Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.