2

I am trying to pull a variable that is captured from a form element in to a function chain. The variable value needs to act as a function name. Example:

<form name="myform">
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="monday" />Monday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="tuesday" />Tuesday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="wednesday" />Wednesday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="thursday" />Thursday
  <input type="radio" id="DayOfWeek" name="DayOfWeek" value="friday" />Friday
</form>

script:

var **monthday**=$("input[name='DayOfWeek']:checked").val();
NewDate = Date.parse(NewDate).add(1).month().**monthday**();

My problem has been getting the variable 'monthday' value to act as the function name in that chain. Am I having a 'duh' moment or is this not possible?

1
  • remember, in js objects are "Dictionaries", and everything is object Commented Feb 10, 2011 at 17:17

2 Answers 2

2

How about this:Date.parse(NewDate).add(1).month()[monthday]();?

Sign up to request clarification or add additional context in comments.

1 Comment

perfect, thanks. Was trying to string a couple together and that's where I had my problem. Should have taken out the dot that was separating the bracketed values.
2

Use bracket notation:

var monthday = $("input[name='DayOfWeek']:checked").val();
NewDate = Date.parse(NewDate).add(1).month()[monthday]();

1 Comment

you guys rock! kind-f a duh moment there. I was stringing together two captured variables and was doing: NewDate = Date.parse(NewDate).add(1).month()[monthday]().[monthrate](); needed to take out that second dot. Thanks for the quick responses!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.