Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Dynamic Functions

    I would like to take the following function and possibly pass it a parameter so that it would stand alone as a separate function.

    Code:
    function tmt_validatorInit(){
    	var formNodes = document.getElementsByTagName("form");
    	for(var i=0; i<formNodes.length; i++){
    		if(formNodes[i].getAttribute("tmt:validate") == "true"){
    			// Attach a validator object to each form that requires it
    			formNodes[i].tmt_validator = new tmt_formValidator(formNodes[i]);
    			// Set the form node's onsubmit event 
    			// We use a gigantic hack to preserve exiting calls attached to the onsubmit event (most likely validation routines)
    			if(typeof formNodes[i].onsubmit != "function"){
    				formNodes[i].onsubmit = function(){
    					return tmt_validateForm(this);
    				}
    			}
    			else{
    				// Store a reference to the old function
    				formNodes[i].tmt_oldSubmit = formNodes[i].onsubmit;
    				formNodes[i].onsubmit = function(){
    					// If the existing function return true, send the form
    					if(this.tmt_oldSubmit()){
    						return tmt_validateForm(this);
    					}
    					return false;
    				}
    			}
    		}
    	}
    }
    I want to do this so that I can do the following upon adding dynamic rows:

    tmt_validatorInit(num);

    num is the variable that gets incremented as you add a row. So it will initialize a new tmt_validator for every dynamic row. I just don't know enough javascript to do it myself and I was hoping that a Javascript Guru could help with this problem.

    Please help me resolve this issue as it is been a problem for several days.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not enough information. What rows are you talking about? Rows in a table? Rows on your html page? Some other row?

    What I see is a function called tmt_validatorInit() that looks through a whole html document and finds each form. Then it attaches a new validation test to that form if it has a certain attribute set. It doesn't do anything with table rows.

    So then you ask how it can be made to work with dynamic rows and I have no answer. I suspect no one else does either (but have been wrong before).

    If you are adding new forms dynamically that would make more sense. Are you?

    david_kw
    Last edited by david_kw; 12-14-2006 at 06:36 PM.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    David,

    I figured out the problem with the crashing of the browser. It seemed when I was closing the </strong> tag, DreamWeaver put a closing </script> tag instead. I am no longer having problems with that, thanks for the attempt and sorry for the lack of information, it wasn't intentional.

    New Problem:

    I created a page to simulate the problem I am having with my application which you can visit at: http://7079.net/js/index.html

    If you click the "Add Account" button, it adds a row. I limited the script to one addtional row based on this particular application. And if you click "Remove Account", it removes the newly added account. Both buttons work fine with the adding and removing. A new problem has been encountered which is calling a function from the tmt validator recursively causing a "Stack Overflow". I am not sure if you use the Javascript Console in FireFox or not, but that's how I debug my Javascript, you may have something better than that that can show you that error.

    Code:
    // Create all the validator objects required inside the document
    function tmt_validatorInit(){
    	var formNodes = document.getElementsByTagName("form");
    	for(var i=0; i<formNodes.length; i++){
    		if(formNodes[i].getAttribute("tmt:validate") == "true"){
    			// Attach a validator object to each form that requires it
    			formNodes[i].tmt_validator = new tmt_formValidator(formNodes[i]);
    			// Set the form node's onsubmit event 
    			// We use a gigantic hack to preserve exiting calls attached to the onsubmit event (most likely validation routines)
    			if(typeof formNodes[i].onsubmit != "function"){
    				formNodes[i].onsubmit = function(){
    					return tmt_validateForm(this);
    				}
    			}
    			else{
    				// Store a reference to the old function
                                      // This is the problem that is causing the error
    				formNodes[i].tmt_oldSubmit = formNodes[i].onsubmit;
    				formNodes[i].onsubmit = function(){
    					// If the existing function return true, send the form
    					if(this.tmt_oldSubmit()){
    						return tmt_validateForm(this);
    					}
    					return false;
    				}
    			}
    		}
    	}
    }
    If there is further information required please let me know as I need to fix this problem as soon as possible.

    Thanks in advance!
    Last edited by 7079; 12-14-2006 at 10:23 PM.

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Check out http://7079.net/js/index2.html

    This script will allow you to add more than just one row. The problem occurs if the "Add Account" button is clicked more than once.

    I really would like to figure out how to solve this problem so that it won't plague my app any longer.

    Thanks again!

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all it desn't crash for me in IE7 or FF2. What browser are you using?

    Second, from what I can see you don't want to call tmt_validatorInit() each time in addEvent().

    All tmt_validatorInit() does is add an onsubmit function to the form. And there is only one form in the html file so if you call tmt_validatorInit() on the initialization of the page that is enough.

    window.onload = function () {
    tmt_validatorInit();
    }

    The function addEvent() doesn't add any new forms. All it does is add a new table.

    david_kw


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •