Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > this.form object

Reply
Thread Tools

this.form object

 
 
mike
Guest
Posts: n/a
 
      10-18-2005
I'm drawing a blank on this.

I have html like:

<form name="test1">
<input name="inp1" onclick="alert(this.parent.form);">
<a href="javascript:void(null);"
onclick="alert(this.parent.form);">click</a>
</form>

when I click the input I get [object], when I click the link I get
undefined.

I want to get the [object] when I click on the link and obviously
missing something.

Mike

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      10-18-2005
mike wrote:
> <input name="inp1" onclick="alert(this.parent.form);">


Try:
this.form
(every input has a 'form' property which refers to the form containing it)

> <a href="javascript:void(null);"
> onclick="alert(this.parent.form);">click</a>


Try:
this.parentNode
(the parent node *is* the form tag)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-18-2005
mike wrote:

> I have html like:
>
> <form name="test1">
> <input name="inp1" onclick="alert(this.parent.form);">
> <a href="javascript:void(null);"
> onclick="alert(this.parent.form);">click</a>
> </form>
>
> when I click the input I get [object], when I click the link I get
> undefined.
>
> I want to get the [object] when I click on the link and obviously
> missing something.


`a' elements that are descendants of `form' elements are not part of
the forms element's collection nor do they have a `form' property;
in short, apart from their position in the parse tree, there is no
connection between link and form in the DOM.

I wonder even why `this.parent' works in the first place. Maybe in IE,
as a proprietary property of its DOM, certainly not the W3C DOM
(Moz/FF/Opera) -- that would be `parentNode'.


PointedEars
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      10-18-2005
>`a' elements that are descendants of `form' elements are not part of
>the forms element's collection nor do they have a `form' property;
>in short, apart from their position in the parse tree, there is no
>connection between link and form in the DOM.


Bummer, so if I knew the form name like:

<a href="javascript:void(null);"
onclick="myfunc('test2')">click</a>

function myfunc(obj)
{alert(document.obj);}

but I get undefined, what I want is [object].

alert(document.form[obj]);

does not work either

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      10-18-2005
this.parentNode works in that example, but if I have

<form>
<table>
<tr><td>
<a href="javascript:void(null);"
onclick="alert(this.parentNode);">click</a>
</td></tr>
</table>
</form>

then I don't think it will. I think it returns the td tag object,
right?

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-18-2005
mike wrote on 19 okt 2005 in comp.lang.javascript:

> function myfunc(obj)
> {alert(document.obj);}
>
> but I get undefined, what I want is [object].
>


an object is not it's name.

if obj is an object:

alert(obj) gives [object]
alert(document.obj) gives undefined.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      10-18-2005
>an object is not it's name.

>if obj is an object:


>alert(obj) gives [object]
>alert(document.obj) gives undefined.


Using a link I need to pass to a function the form object.

In my example I had:
<a href="javascript:void(null);" onclick="myfunc('test2')">click</a>

in the function:
function myfunc(obj) {alert(obj);}

I would get "test2". What I want to get is [object], which is the
object of the form test2.

Make sence?

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      10-18-2005
If I do this:

<form name="test1">
<table>
<tr><td>
<button onClick="alert(this.form);alert(this.form.name);"> Click
Me</button>
</td></tr>
</table>
</form>

then I get [object], test1.

Yep, i am using IE. the <a> tag doesn't give me the form, but the
button does.

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-18-2005
On 18/10/2005 23:00, mike wrote:

[snip]

> <a href="javascript:void(null);"


Do not use the javascript: pseudo-scheme in href attributes. See
<URL:http://www.jibbering.com/faq/#FAQ4_24>.

> onclick="myfunc('test2')">click</a>
>
> function myfunc(obj)
> {alert(document.obj);}


That would display the value of the obj property of the document object,
type-converted to a string.

> but I get undefined, [...]


Because there is no such property.

> alert(document.form[obj]);


That would attempt to display, assuming the call shown at the start of
your post, the test2 property of the form object in the document object.
That is, it's equivalent to:

alert(document.form.test2);

> does not work either


That's because there's no form property, but you are closer.

All form controls have a form property. As you now know, this is an
object reference to the FORM elements that contains the control.

The document object has a forms (notice the 's') property that is a
collection of all FORM elements in the document. This collection can be
indexed by id, name, or ordinal number. So, to access a form with the
name or id attribute value, test2, one would write:

document.forms.test2

If the identifier was a string value in a variable, that would be
altered to:

document.forms[myFormId]

See <URL:http://www.jibbering.com/faq/faq_notes/form_access.html> for
more information on accessing forms and form controls.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      10-18-2005
yes this is what i was looking for:

document.forms[myFormId]

about your comment on:

> <a href="javascript:void(null);"


what do you suggest I use? buttons are too big and take up valuable
real estate on the page. I guess I could use more images. users know
that links are "clickable"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments