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 to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Attaching javascript function on submit of form

    Hello All,

    I am trying to attach javascript function using javascript code which gets call when form submitted. here is the code which i am using to attach javascript function.
    ======
    function klevu_addEvent( obj, type, fn ) {

    if ( obj.attachEvent ) {

    obj['e'+type+fn] = fn;

    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}

    obj.attachEvent( 'on'+type, obj[type+fn] );

    } else{

    obj.addEventListener( type, fn, false );
    }
    }

    where obj = form object , type= type of event you want to attach (for FORM it will be submit), fn= function that you want to call on form submit.

    above code is not working. it's not calling function on form submit.

    does any one know javascript code which listens on submit event of particular form.

    Many Thanks,
    Dhaivat

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,249
    Thanks
    15
    Thanked 254 Times in 254 Posts
    The best way I've found to do that is to let the submit button, it doesn't even have to be a real submit button, or even part of the form, but the button onclick calls a function which does any 'chores' that need to be done, then it submits the form. For instance in my latest form, the inputs are not in an actual form, but a 'phony, one, all the input values are read with JS, assembled into an array, the array becomes the value of a hidden input in a real but invisible form, and that form is submitted at the end of the function. It sounds complicated but it really isn't, all the data goes in as one chunk, the array, and comes out as one that is made back into another array. This works very well when you need to 'push' multiple sets of input values into the array.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey DrDOS,

    Many thanks for your reply. now in my case i have page already generated with FORM and i need to attach the javascript function on "onsbumit" function. i don't have any access to this page except page will import my javascript and using this javasctipt i have to obtain the form (using id param) and need to attach javascript function dynamically.

    any idea how this possible?

    Many Thanks,
    Dhaivat

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,249
    Thanks
    15
    Thanked 254 Times in 254 Posts
    Code:
    document.getElementById("theID").onmousedown=myFunction;
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,984
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    document.getElementById("your_form_id").onsubmit=function(){
    alert("some function stuff here");
    // return false // if you don't want the form to submit
    }
    script has to be appended to the body of the document, not the head...


  •  

    Posting Permissions

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