0

I am currently working within the confines of sharepoint 2007 and have some code that turns a dropdownlist into a combobox. What i have works perfectly fine, however this code is meant to be used by business analysts and content creators so it has to be as simple as possible. So i am condensing it into a function and all has gone swimmingly except for the onChange event.

I have managed to extract the original onChange event, though due to my dropdownlist i have to replace a parameter. So i convert it to a string, replace the parameter and need to reconvert it to a function a la:

var onChangeFunction = "function (){alert("your function has been called")}"    

//The function 'attachEvent' is not common javascript. It is a custom function
//that works for my combobox.
combobox.attachEvent("onChange",(function)onChangeFunction);

Is that even possible?

4
  • That's certainly not valid JavaScript. Commented Oct 12, 2011 at 22:02
  • If you are referring to the code, i am aware of that. I am trying to convey the sort of functionality i would like to achieve. If you are saying that what i want to achieve is not valid javascript then can you elaborate? Commented Oct 12, 2011 at 22:06
  • Try: combobox.attachEvent("onChange",function(){alert("Test")}); Commented Oct 12, 2011 at 22:08
  • the attachEvent function is part of the code for my custom combobox. It isnt something that is out of the box. Commented Oct 12, 2011 at 22:11

1 Answer 1

1

You can use new Function but I think you are on a slippery slope playing with this.

var fn = new Function("{alert(\"your function has been called\")}")
combobox.attachEvent("onChange",fn);
Sign up to request clarification or add additional context in comments.

3 Comments

Im pretty sure this is what i want. And im well aware of the brittle nature of Javascript, but ill make sure to let the users know.
Javascript isn't "brittle", the way you are using it is. The solution offered by Rob W is much better than the one you have accepted (and functionaly equivalent).
@RobG, the question was not how to attach a function, but how to convert a string to a function.

Your Answer

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