Solved

Get form name

Posted on 2004-09-28
16
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 250 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

696 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