Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 4th, 2007, 05:51 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Javascript objects

Hi all,

Just a quick javascript question. I have been playing with objects in JS with some success, but have come accross a bit of a problem, which I cant quite get my head around.

I have a project in which multiple forms are used, so i created a class which would carry out the repetetive processes I was going through for each form. In use, the object is first created, then init is called to add the appropriate event listeners and carry out the required tasks etc. the code is below

Code:
function formHandler(form, url)
{
    //form object
    this.form = form;
    this.url = url;

    /*
    * Function to initialise the form
    */
    this.init = function() {

        //Add the highlightors
        this.addHighlightor(this.form.getElementsByTagName("input"));
        this.addHighlightor(this.form.getElementsByTagName("select"));
        this.addHighlightor(this.form.getElementsByTagName("textarea"));

        //Give focus to the form
        Form.focusFirstElement(form);

        //Add the onsubmit handler
        form.onsubmit = this.validate;


    }


    /*
     * Function takes an array of objects and adds a highlightor event to their parent nodes
    */
    this.addHighlightor = function(elems) {
        for (var i = 0 ; i < elems.length ; i++) {
          elems[i].onfocus=function(){
            if (this.parentNode.className != "submit")
                this.parentNode.className+=" active";
          };
          elems[i].onblur=function(){
            if (this.parentNode.className != "submit") {
                //Get original class name
                var name = this.parentNode.className.split(" ");
                if (name[1] == undefined) {
                    this.parentNode.className=name[1];
                } else {
                    this.parentNode.className="";
                }
            }
          };
        }
    }

    this.test = function() {
        alert('test');    
    }

    this.test2 = function() {
        alert('called');
        this.test();    
    }

    /*
     * Function validates the inputs within the form if there is a class name of required
     * @return
    */
    this.validate = function() {

        //Use prototype to obtain all the elements in the form
        var elements = Form.getElements(form.id);


        for (var i = 0 ; i < elements.length ; i++) {
            if(elements[i].className.indexOf('required') != -1) {
                if (!Field.present(elements[i])) {

                    //find the label
                    for(var y=0 ; y < elements[i].parentNode.childNodes.length ; y++) {
                        if (elements[i].parentNode.childNodes[y].tagName == "LABEL") {
                            var desc = elements[i].parentNode.childNodes[y].innerHTML;
                        }
                    }

                    alert(desc+" must be entered");
                    elements[i].style.backgroundColor = "#FFC7D4";
                    elements[i].focus();
                    return false;    
                }
            }
        }
        this.test2();
    }
}
The problem I am having is with the onsubmit event listener for the form. The vaidate function is performed onsubmit, but when it reaches this.test2() it tells me it isnt a function and dies.

After ruling out typos etc I think this is something more fundamental that I am not understanding so any help would be greatly appreciated!

Many many thanks

/Matt
Reply With Quote
  #2 (permalink)  
Old February 11th, 2007, 09:39 PM
Friend of Wrox
 
Join Date: Nov 2005
Location: Rotterdam, , Netherlands.
Posts: 223
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why don't you just delete that function it doesn't seem to serve any purpose other then alert anything.

__________________________________________________ ________
I am DJ Kat...that's my name. Its a D and a J and a Kat with a K.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stop current javascript function using javascript sakthi Javascript 3 June 2nd, 2008 03:30 PM
Tab objects in Javascript ttbhandari Javascript How-To 1 February 11th, 2005 05:37 AM
Javascript Objects Source Code? mikedshaffer Javascript 3 September 17th, 2003 01:17 PM



All times are GMT -4. The time now is 05:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.