Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtain form name or ID inside function

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
If I have a function called submitAction() with no variables, can I get any information from the form, like the ID or name of the form if the function is in the "onsubmit" event?

Secondly, if I have a function submitAction() and attach it to the onClick event of a link, can I obtain information about that link like the ID or anything, without actually putting variables into the function reference? (like submitAction('var1','var2'))
 
Yes, and yes, although don't forget you could pass "this" through as a parameter should you need to have it generic enough to work across many placements of the code.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
well, i the issue is i'm actually attaching the onsubmit event to forms on my page. the code kinda works like this

onload > attachSubmit(the_form);
Code:
function attachSubmit(form_ref){
    if(window.addEventListener){
        form_ref.onsubmit = submitAction;
    } else {
        form_attach.attachEvent('onsubmit',submitAction)
    }
}
And if i try:
Code:
function attachSubmit(form_ref){
    if(window.addEventListener){
        form_ref.onsubmit = submitAction(form_ref);
    } else {
        form_ref.attachEvent('onsubmit',submitAction(form_ref))
    }
}
For some reason it executes the function submitAction() right then.
 
You should use the first syntax, I'd say - although you're using "form_ref" in the upper branch and "form_attach" in the lower - is this right? Is "form_attach" defined anywhere?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
sorry, just a mere example error, they're supposed to both be the same. So, if I use the first example, how do i found out what form is being submitted without any arguments?
 
Ok, I figured out the form part (this.form and this.name) when used with the onSubmit action. What about the Link that sits inside a form. It must be this.[something].

BTW, I tried this code
Code:
for(items in this){
    alert(this[items]);
}
and I got a lot of seemingly useful information, however I couldn't figure out what "index" or reference to use to get the value that I saw in my alert. Thanks for the help!
 
Ok, i actually thought I figured it out, but I really didn't. this.id and/or this.name don't work in IE. So it got me thinking. I'd like to get the document.form[index] of this form. That's going to be the best way in case the form doesn't have an ID or a name (for some strange reason)...

So can you help me with this?
 
You'd have to inspect the event data (which is passed as an argument in non-IE browsers, and is available as a global variable, "event" in IE).

However, you might find it easier to pass "this" through given the syntax in your first function:

Code:
<form name="form1" onsubmit="doSomething(this);">

...

<form name="form2" onsubmit="doSomething(this);">

...

function doSomething(whichForm) {
   // "whichForm" contains a ref to the form that called this function
}

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I did figure out a way to pass a variable to my onSubmit function by doing this:

Code:
if(window.addEventListener){
    form_attach.onsubmit = function(){ return submitAction(form_ref); };
} else { // IE
    form_attach.attachEvent('onsubmit', function(){ return submitAction(form_ref); });
}

But the issue is I have to 2 submitActions, 1. submitAction and 2. submitLinkAction. I'm trying to combine these into one but to do that, i have to be able to extract the name/id/index of the form from inside the function without any variables. I also will need to be able to find out the tagName of the element that clicked. Here's the jist of what i would like to do:
Code:
function submitAction(){
    //run validator
    process = validator; //returns true/false
    //reference the form by index
    document.forms[this_form]; //just in case there's no form ID or name.
    //get the tagName of the element that processed this function
    tag = this.tagName or window.event.srcElement.tagName

    if (tag == "A" OR (tag == "INPUT" && this.type != "submit")) AND process)
    document.forms[this_form].submit()
    else return false;
    
    return process; // this will return the value if it's an actual submit button.
}
I know it's not real code, but it's the best way I know to explain it.
 

use anonymous functions, that will prevent from firing the functions you want to assign to the onsubmit event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top