www.webdeveloper.com
Results 1 to 3 of 3

Thread: Please help me adapt this code to all forms of a page

  1. #1
    Join Date
    Jul 2014
    Posts
    3

    Please help me adapt this code to all forms of a page

    Hi; i have managed to make a javascript code that grab a form by id and add an input to it after user click submit button. Now i want to adapt this code and especially continue to use my function addEvent to add the input to the form that the user will click whatever its id is and doesn't mater if it have an id or no, please i don't use jQuery so please just help me adapt my code below :
    HTML Code:
    var forms = document.getElementById('my-form');
    
    function addHidden(theForm, key, value) {
        // Create a hidden input element, and append it to the form:
        var input = document.createElement('input');
        var time_now = cmsarmor_get_timestamp();
        input.type = 'hidden';
        input.name = key;
        input.value = value;
        theForm.appendChild(input);
    }
    
    function processForm(e) {
        //if (e.preventDefault) e.preventDefault();
        addHidden(forms, 'key-one', '33');
        /* do what you want with the form */
        // You must return false to prevent the default form behavior
        return true;
    }
    
    function addEvent(elem, event, fn) {
        // avoid memory overhead of new anonymous functions for every event handler that's installed
        // by using local functions
        function listenHandler(e) {
            var ret = fn.apply(this, arguments);
            if (ret === false) {
                e.stopPropagation();
                e.preventDefault();
            }
            return(ret);
        }
    
        function attachHandler() {
            // set the this pointer same as addEventListener when fn is called
            // and make sure the event is passed to the fn also so that works the same too
            var ret = fn.call(elem, window.event);   
            if (ret === false) {
                window.event.returnValue = false;
                window.event.cancelBubble = true;
            }
            return(ret);
        }
    
        if (elem.addEventListener) {
            elem.addEventListener(event, listenHandler, false);
        } else {
            elem.attachEvent("on" + event, attachHandler);
        }
    }
    addEvent(forms, "submit", processForm);
    Thank you

  2. #2
    Join Date
    Aug 2014
    Location
    Clark Freeport Zone, Philippines
    Posts
    2
    change:

    var forms = document.getElementById('my-form');

    to

    var forms = document.getElementsByTagName('form')

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,374
    What do you mean by adapt to all forms? You need to supply some HTML code to illustrate the form(s) you want to adapt.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles