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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect in whitch <form> an element is? 2

Status
Not open for further replies.

zfs

Programmer
Joined
Feb 23, 2006
Messages
7
Location
EE
Say I have multiple forms in my page and also multiple elements in each form. Now I need this: I know an element's id and I want to get the form's id the element is in.

Background: I need this to be able to dynamically add elements (hidden fields) to the same form that the desired element is.

Ofcourse I could loop through all document.forms, but maybe thers an easier way? Secondly I probably could loop thruogh all element's parentNodes until I find the form, but yet again maybe I'm just missing a very simple solution?
 
Thanks for that hint. But what if the form has no specified id value? Like this:
Code:
<form>
  <input name="test" id="test" value="Form 1 value" />
</form>
<form>
  <input name="test2" id="test2" value="Form 2 value" />
</form>
How could I detetct the form in that case? I guess I need to find out whitch place (with whitch index) the form is to be able to call document.forms[idx]. In the example the idx can be either 0 or 1.
 
while i can't imagine why this would ever happen (at the very least, an "action" attribute is required), try this:

please not that this is IE-only

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
<script type="text/javascript"><!--
function getThisFormsID( f ) {
    var fC = document.forms;
	for ( var i = 0; i < fC.length; i++ ) {
		if ( fC[i].uniqueID == f.uniqueID ) {
			alert( "index is: " + i );
			return;
		}
	}
	alert("index not found!")
	return;
}
//--></script>
</head>

<body>

<form name="form1"><input type="button" onclick="getThisFormsID(this.form);" name="b1" /></form>

<form name="form2"><input type="button" onclick="getThisFormsID(this.form);" name="b2" /></form>

</body>
</html>



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Why doesn't the form have an id?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
How about just check the identity of the form object.
[tt]
<form>
<input type="button" onclick="checkup(this.form);" value="check form index" />
</form>
<form>
<input type="button" onclick="checkup(this.form);" value="check form index" />
</form>
[/tt]
with the function checkup() like this.
[tt]
function checkup(obj) {
for (var i=0;i<document.forms.length;i++) {
if (obj==document.forms) {
alert("this form's index = "+i); //as illustration
break;
}
}
}
[/tt]
 
To tsuji:
zfs said:
Ofcourse I could loop through all document.forms, but maybe thers an easier way? Secondly I probably could loop thruogh all element's parentNodes until I find the form, but yet again maybe I'm just missing a very simple solution?
 
Thanks cLFlaVA, but I think you misunderstood what I want to acomplishe. I admit, that my description and also english aren't the best, but maybe read the frist post again. I try to write a more complete example that is suppoused to add an additional input field to the same form where element with id="test2" is:
Code:
<form action="?whatever" method="POST">
  <input name="test" id="test" value="Form 1 value" />
</form>
<form action="?whatever" method="POST">
  <input name="test2" id="test2" value="Form 2 value" />
</form>

<script type="text/javascript">
  // find out in whitch form the element is
  var elem = document.getElementById('test2');
  var form_index = [b]get_it_somehow(elem)[/b];

  // create new element and append it to the same form
  var new_elem = document.createElement('input');  
  document.forms[form_index].appendChild(new_elem);
</script>
So the question is, how should the get_it_somehow(elem) function look like?
 
just use the .form attribute, as Dan said above...

Code:
<script type="text/javascript">
  // find out in whitch form the element is
  var elem = document.getElementById('test2');
  var the_form = elem.form;

  // create new element and append it to the same form
  var new_elem = document.createElement('input');  
  the_form.appendChild(new_elem);
</script>



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks cLFlaVA and also Dan! I knew there was a solution too easy to come to [thumbsup2]
 
zfs,

Just so you know what you're dealing with, what you actually wanted was a reference to the form element / form object itself, not its ID.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top