What's the best way to get a string to act like an array/object in jQuery?
-
The plugin can be custom configured with multiple objects that define default values. The names of those objects will be developer defined - not predefined. Then based on "input" (e.g., form, parsing the CSS ids and classes, etc.) I want to use an "input" value to get the corresponding default object. (In case it's not clear, the input value would be a string of the same characters of a default object name.) Once I have the default values for the given input / object, I would proceed with using them in the plugin. Currently, when I try to .each the default parms when defining it with the "input" value, each parses that string, not the object the value of the string is asking for. Make sense? Pardon me if my jargon isn't accurate I'm somewhat new to this area of jQuery. In a nut shell, I'm looking for the jQuery equivalent of PHP $$ (double dollar sign).
-
Answer:
I've created a demonstration of my best guess based on my understanding of what you've described: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-git.js"></script> <script type='text/javascript'>//<![CDATA[ $(function(){ var object = { "hi": "hello!", "bye": "good-bye!" }; $("#theinput").on("keyup", function() { var message = object[ $(this).val() ]; if ( message ) { $("#themessage").html( message ); } }); }); </script> </head> <body> Type: "hi" or "bye" <input type="text" id="theinput" value=""> <div id="themessage"></div> </body> </html> Live demo can be found here: http://jsfiddle.net/rwaldron/wZgCn/
Rick Waldron at Quora Visit the source
Related Q & A:
- What's the best way to get into the Graphic Design industry?Best solution by Yahoo! Answers
- What's the best way to get a job in a restaurant?Best solution by Yahoo! Answers
- What's the best way to get smudges off of a plasma?Best solution by Yahoo! Answers
- What's the best way to get from Seoul intl airport to Nam dae moon? Taxi cost? or bus?Best solution by Yahoo! Answers
- What's the best way to get followers on Twitter?Best solution by michaelhyatt.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.