views:

28

answers:

2

I have a web app which has two text areas. When one text area receives a mousedown event, a variable "side" is set, either "left" or "right." When a user selects some text in a text area, three strings are made. One for the text before the beginning of the selection, the selection itself, and the text after the selection to the end. A function is set to return these like this:

return { head: head_text, tail: tail_text, sel: sel_text, side: text_side }

Now, I have created an array, and I want it to appear in such a way that we get, text.left({"head":"four score", "selection":"and seven", "tail":"years ago."}) I am assuming I would do this by text.side = getSelection(), but how do I get it to evaluate the variable "side" instead of thinking of it as an object within "text"?

EDIT: Ok, just to clarify, I might be completely wrong in my ideas in how this works, but here it goes. I want to make it so that a function can look at "text" see within text two objects, "left" and "right," and then evaluate the head, sel, and tail of each object. Would it be easier for me to use two objects?

A: 

I'm not completely sure I understand the question. However, I think you want to use a string to access a property of an object.

Javascript allows you to this like a dictionary:

text["head"] = "four score"; // set's "four score" to the head property of text

or

text[side] = "your selection"; // where side is a property name

Edit:

In answer to your edit, it would be easier to create one object with two properties and assign each of those properties an object. Like the following:

var text = {};
text.left = getHeadSelTail("left");
text.right = getHeadSelTail("right");

You can now access the left head using

text.left.head = "four score";

And the right head using

text.right.head = "erocs ruof";
Joel Potter
A: 

Am I correct assuming that you want an array containing the values of the fields head, selection and tail ? That you can do using

var myObject = ....// the method with the return statement from your question
var myArray = [myObject.head, myObject.selection, myObject.tail];

Now if you want the complete string again you can use

var myText = myArray.join("");
Sean Kinsey