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

    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;
    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) {
        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;
        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
    Clark Freeport Zone, Philippines

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


    var forms = document.getElementsByTagName('form')

  3. #3
    Join Date
    Mar 2007
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

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