/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Problem passing form name in Chrome and Firefox, not IE8

Hi there,

I am using dynamically generated forms with (unknown) unique names and id’s. When I want to pass the form id with javascript to a function, I receive a message from Chrome (in the javascript console) that it can not read the property id of null.

I investigated this some more, and found that chrome and firefox are not passing the parameter this.form to a function correctly.

A little sample code:
<FORM name=”[I]variablename[/I]” id=”[I]variablename[/I]“>
<input type=”text” name=”[I]variablename[/I]” id=”[I]variablename[/I]” onclick=”test(this.form);”/>
</FORM>

(variablename stands for an automatically generated name which I can not literally state when calling the function)

The javascript function ‘test’ looks like this:
function test(form) {
var formid=form.id;
alert(formid);
}

Normaly a popup should arise stating the id of the form. In IE it works, but in Chrome and Firefox it doesn’t.

What am I doing wrong here? ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PadonakJun 24.2010 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Form ID</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript" type="text/javascript">
function test(form){var formid = form.id; alert(formid);}
</script>
</head>
<body>
<form name="form_1" id="form_1" action="">
<input type="text" name="variablename_2" id="variablename_2" onclick="test(this.form);" />
</form>
</body>
</html>[/CODE]


i just have tested this code in 3 browsers: IE6,FF and Opera. everything works good. your problem isn't here.
Copy linkTweet thisAlerts:
@FangJun 24.2010 — Can you give an example of how the form is generated?
Copy linkTweet thisAlerts:
@funzohauthorJun 25.2010 — I have figured out what was wrong. Padonak was absolutely wright. My problem was not the passing of this.form to the function, but was a mistake on my side of putting the form tag between the table "tag" and the first "tr" tag.

When you do this, IE is still able to "form" tag, while mozilla isn't.

I didn't mention it in the short code in this post, just to keep the code... short. My mistake again! Padonak, your remark made me strip the code down and test is bit by bit when suddenly...

For those who are interested I wrote a little peace of wrong and wright code...

WRONG CODE:

<table>

<FORM name="variablename" id="variablename">

<tr>

<td>

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

</td>

</tr>

</form>

</table>

WRIGHT CODE:

<FORM name="variablename" id="variablename">

<table>

<tr>

<td>

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

</td>

</tr>

</table>

</form>

Regards,

Funzoh
Copy linkTweet thisAlerts:
@dominicbri7Mar 23.2011 — I just tested this, and it seems to work on Chrome 10.0.648.151.

What seems weird is that in the application I am working on, it doesn't work.

Can anyone else confirm it?

[CODE]<table>
<form id="myForm">
<tr>
<td>
<input type="button" name="monBouton" value="A button..." onclick="alert(this.form.id);">
</td>
</tr>
</form>
</table>[/CODE]

Actually alerts 'myForm' as it should, but doesn't work in your case, right?
×

Success!

Help @funzoh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...