Solved

Get form name

Posted on 2004-09-28
16
335 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
Comment Utility
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
Comment Utility
i think you should use:

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


viola
0
 
LVL 17

Author Comment

by:Tacobell777
Comment Utility
??

Zyloch - you serious? It errors for me. let me try on another page...
0
 
LVL 36

Expert Comment

by:Zyloch
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Author Comment

by:Tacobell777
Comment Utility
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
Comment Utility
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
Comment Utility
document.getElementsByTagName("form")[0].name
0
 
LVL 17

Author Comment

by:Tacobell777
Comment Utility
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
Comment Utility
What about document.forms['formname']?
0
 
LVL 3

Expert Comment

by:hemebond
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
yes thats the one I was looking for... thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now