Vranilac
Vranilac

Reputation: 79

How to take form name, from which, on submit, is call javascript function in this jscript function and then use name in same js function as variable

I have to call same javascript function (chek() ) from three form's on onSubmit event and need to know which one form called this function. It is three buttons. How I can transfer name of form in javascript function chek(), which one is called on submit form, and then in javascript do something with name of form as variable. Example:

<form id="one" action="" method="post" onSubmit="chek();">
<input type="hiden" id="idfi11" name="idf1">
<input type="submit" id="idfi12"> value="B1" >

<form id="two" action="" method="post" onSubmit="chek();">
<input type="hiden" id="idfi21" name="idf1">
<input type="submit" id="idfi22"> value="B2" >

<form id="three" action="" method="post" onSubmit="chek();">
<input type="hiden" id="idfi31" name="idf1">
<input type="submit" id="idfi32"> value="B3" >

This is three buttons and depend of which one is click javascript chek() chek something and count some result. Example javascript:

<script type="text/javascript">
function chek()
{
var message1,message2,message3="";
var data="";
 ... count data string, and message1,message2, messag3....

 if (callname = form one) // here I need name of called form
    {
    alert("Message one :"+ message1);
    document.getElementById("idfi11").value = data;
    }
 if (callname = form two)
    {
    alert(Message two : + message2);
    document.getElementById("idfi21").value = data;
    }  
if (callname = form three)
    {alert("Message three :"+message3);document.getElementById("idfi22").value = data;}
return (true);
}
</script>

Thank You very much for reply...

Upvotes: 0

Views: 261

Answers (3)

Tristup
Tristup

Reputation: 3673

Javascript :

chek(this);

Jquery :

$(element).closest("form").attr("id");

Upvotes: 0

Vranilac
Vranilac

Reputation: 79

I do this

<form id="one" action="" method="post" onSubmit="chek(this);">
...

Javascript

function chek(vforma)
{   
alert(vforma.id); // result= 'one'
...    
}

Upvotes: 0

rajesh kakawat
rajesh kakawat

Reputation: 10896

Try something like this

HTML

<form id="one" name="one" action="" method="post" onSubmit="chek(this);">

JAVASCRIPT

function chek(obj){
    alert(obj.id);// will give you ID of form
    alert(obj.name);// will give you NAME of form
}

Don't forget to add name attribute to form.

Upvotes: 3

Related Questions