?
Solved

Get form name

Posted on 2004-09-28
16
Medium Priority
?
345 Views
Last Modified: 2008-02-01
I'd like to get a form name dynamically (I know its possible, as I have done it before, but lost the code and forogt how to do it).

Example

<form>
<input type="text" onclick="fn( this.form.name );"/>
</form>

so instead of passing it the form name fn( 'formName' ); I want to get it dynamically, make sense?
0
Comment
Question by:Tacobell777
  • 6
  • 5
  • 2
  • +3
16 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12175138
Hi Tacobell777,

I don't get it. If you pass it

fn(this.form.name), you will pass it the form name...

Regards,
Zyloch
0
 
LVL 6

Expert Comment

by:viola123
ID: 12175326
i think you should use:

<form>
<input type="text" onclick="fn( this.name );"/>
</form>


viola
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12175489
??

Zyloch - you serious? It errors for me. let me try on another page...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 36

Expert Comment

by:Zyloch
ID: 12175508
It could be because your form doesn't have a name... Try this:

<form name="myName">
<input type="button" onclick="alert(this.form.name);" value="Click to Test" />
</form>
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12175574
ok, the problem I guess is that the statement is not on a form element, it is on a td

<th scope="col" class="sortable" onclick="alert(this.form.name);">

How to solve that issue?
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12175652
If you're expecting it to be in a form in your function, shouldn't it be in a form, not a table?
I'm slightly confused here, what exactly are you trying to accomplish?
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12175744
I have a table in a form, on that table I have a function which expects the form name.
This function is in an include file, I'd like it to see dynamically what form it is in, instead of having to specify it as a string everytime...

In the function I have something like eval('document.' formName ......

Make sense now?
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12175752
Can you put the function on a form element instead of a table. Can you give us a general idea of what you're trying to accomplish, like shade the td this color, or whatever? You could try using

parentElement.parentElement.parentElement
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12175862
No I cannot put the function on a form element instead, basicaly the td contains the header of the column, once clicked it sorts the results either asc or desc - following is the function

function fnChangeSortOrder( form, sortBy )
{
      if ( form.sortOrder.value == 'asc' )
      {
            form.sortOrder.value = 'desc';
            form.sortBy.value = sortBy;
      }
      else
      {
            form.sortOrder.value = 'asc';
            form.sortBy.value = sortBy;
      }
      form.submit();
}

see the form passed in? I want to change that to something like
var myForm = eval('document.' & form ........
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12175869
you could do document.forms[0] or document.forms['formname'] or are you not able to do that?
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12175977
document.getElementsByTagName("form")[0].name
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12176048
document.forms[0]  = no because it does not have to be the first form on the page
document.getElementsByTagName("form")[0].name = same thing as per above
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 12176056
What about document.forms['formname']?
0
 
LVL 3

Expert Comment

by:hemebond
ID: 12176913
document.getElementById("form_id");

Perhaps if you explain exactly what it is you're trying to achieve, we could suggest some alternate methods.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 12176934
I do that like this:

<html>
<head>
<script>
function sortTab(theCell){
  theForm = theCell.parentNode;
  while(theForm.nodeName!="FORM"){
    theForm = theForm.parentNode;
  }
  alert(theForm.name);
}
</script>
</head>
<body>
<form name="myForm">
<table>
<th onClick="sortTab(this)">Click me</th></tr>
</table>
</form>
</body>
</html>

0
 
LVL 17

Author Comment

by:Tacobell777
ID: 12177445
yes thats the one I was looking for... thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question