I'd ideally like to create a streamlined interface which requires the user to select one of three radio buttons such as shown here:
<form id="icon-type">
<input type="radio" name="icon" value="apples" /> Apples
<input type="radio" name="icon" value="oranges" /> Oranges
<input type="radio" name="icon" value="graphes" /> Grapes
</form>
Using jQuery it's easy to determine which of the three have been selected:
var iconType = $('input[name=icon]:checked').val();
However, I need to convert the string assigned to iconType to an previously created object of the same name. I've read that eval()
is the best way to do this, but have also encountered numerous warnings stating that eval()
should never be used. However there seems to be no other way to perform this conversion. Thus my question: while I recognize that eval()
can certainly be misused, is this a case where eval()
is indeed the appropriate approach?
Thoughts much appreciated!
EDIT: @DVK, thanks! Still a bit lost though; suppose my objects look like this:
var apple = new GIcon(baseIcon);
apple.image = "http://localhost/images/apple.png";
var orange = new GIcon(baseIcon);
orange.image = "http://localhost/images/orange.png";
So I need to convert the value retrieved from the selected radio button into the appropriate object reference. How would I store these in a hash as you explain? Thanks!