views:

193

answers:

4

hello, I am in the process of writing a javascript object that contains a method that returns the html of a standard form. In this object I also have a method validate(); I'd like the form generated to use validate();

So the typical html of a form with validation would probably look like this:

<form id="a" onSubmit="return validate();">

The problem is that I need to be able to reference the object instance so it would need to be more like onSubmit="my_object.validate();">

I've tried something like return '<form id="a" onSubmit="return ' + this.validate + '();">'; but I get really strange behavior.

If I make the validate function arbitrarily return true the form gets submitted, false it doesn't. If I do any other calculations in the method I get this error:

> Error: syntax error Source Code:
>     return id ==

Has anyone experienced anything like this?

A: 

Use eval() to execute a string as javascript

[EDIT} Sounds then like you need to prototype the form and then in submit call this.validate(). Get a reference to the form object when you create it using javascript and then define your method for validation and assign that to the validate property.

Dave Anderson
There must be a better way than resorting to "eval()".
Steve Harrison
undoubtedly but if you have a string value how else can you turn it into something to execute?
Dave Anderson
@Dave Anderson: You try to avoid ending up with a string value that needs executing... ;)
Steve Harrison
+5  A: 

Rather than outputting the event handler in the HTML attribute, you can output the HTML, get a reference to the form object, then attach an event handler programmatically, like this:

<html>
  <head>
    <script type="text/javascript">
        var my_object = {
            outputForm: function(container) {
                container.innerHTML = 
                    '<form id="a"><input type="submit" value="Validate" /></form>';
                this.createdForm = document.getElementById('a');
                this.createdForm.onsubmit = this.validate;
            },
            validate: function() {
                // use this.createdForm to get at the controls.
                alert("Who dares awake my slumber?");
            }
        };

        function createTheForm() {
            my_object.outputForm(document.getElementById('container'));
        }
    </script>
  </head>
  <body onload="createTheForm()">
    <div id="container"></div>
  </body>
</html>
Jacob
A: 

sorry for posting this as an answer, after registering it wouldn't let me edit my original post as non registered user?

I thought about eval but I'm not sure how to even use it in this situation? I've tried it like so

' onSubmit="return eval(' + this.validate+'();)">';

and some other variations but I get the same error.

I would like to avoid having to manually add the event handling as I would like it to be pretty self contained. I was thinking about setting up a regular function which sits outside of the object and then doing something like

' onSubmit="return my_function(' + this + ');">';

then in my_function do this:

my_function(given){ return given.validate(); }

this seems like an awful hack and I'm not even sure if it will work.

Ori Cohen
A: 

Why are you not just applying it to the element after you add it to the page and using a closure to keep scope?

var myId = "bar" + index;
foo.innerHTML="<form id='" + myId + "'>...</form>";
var that = this;
document.getElementById(myId).onsubmit = function(){ that.validate(this); }

Adding event handlers to the markup is always a bad idea.

epascarello