homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Conflict between form and javascript

 1:10 pm on Oct 12, 2008 (gmt 0)

Hi all,

I really can not understand whether is it a normal situation, or is it a real conflict?

I have form elements, where client can enter different values, select checkboxes and other stuff. All the entered and chosen data is calculated with simple javascript, to produce the final result depending on selected values.

Everything works fine, until I add <form ......> and </form> tags around my html code. As soon as I add them, there is a message in status bar of the browser telling me about mistake ("<variable> is not defined"), and it refuses to calculate!

I can't see any reason for that... Any guesses? Why does the script work when there are no <form> tags, and how can simple <form> tag affect the whole stuff so badly?!

here is the working code:
...but if I remove <!-- --> near first <form..> and </form> tags, it gives an error and stop working... why?

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html">
<form method="post" action="mailer.php">
<p>Choose option:
<select id="option" name="option" onChange="calculate();">
<option value="6000">op1</option>
<option value="5000">op2</option>
<option value="4000">op3</option>
<option value="3000">op4</option>
<p>Enter number:
<input id="number" name="number" type="text" value="0" onChange="calculate();">
<input id="check" name="check" type="checkbox" onClick="calculate();">
<input id="res" name="res" type="text" disabled>
<input type="submit" name="Submit" value="submit">
<script language="JavaScript">
var timer,x1,x2,x3,result;
function calculate()
if (x2 <= 10) {x2=500}
else if (x2 > 10 && x2 < 50) {x2=1500}
else {x2=3000}



 2:05 pm on Oct 12, 2008 (gmt 0)

Dunno if it is related, but you ought to have a Radix parameter on your parseInt() calls [otherwise results are unpredictable, and dependant on the content of the first parameter].

You ought to have a ";" in {x2=500}, {x2=1500} and {x2=3000}

I'm not sure that isNaN() on the result of parseInt() is going to give you the desired effect.


 12:24 am on Oct 14, 2008 (gmt 0)

I assume you are trying this in IE, as I don't think other browsers will give you this error, but anyway... you shouldn't really be accessing your form elements in the global scope and there lies your problem. Once you add your form container, option.value (and your other elements) are no longer available (in IE). You need to access them via the forms array, since they are now contained within a form. Such as:


However, since you have IDs on your form elements, you could consider using the newer DOM methods...



 5:20 am on Oct 14, 2008 (gmt 0)

Krispy2, thanks, I really forget to add ";"!
though still, it did not affect script, because it worked well (before adding form).

penders, thank you very much! that's it!
This situation also happens in Opera 9.22 (without details about error, but no calculations and empty "result" field, so the problem exists).
very good advice. Now I will rewrite the whole page using DOM and tell about results here.


 6:15 am on Oct 14, 2008 (gmt 0)

"though still, it did not affect script, because it worked well (before adding form)."

Yup, I was just being pedantic, but if you compress/minify the script it might become wrongly interpreted.

I find putting JS scripts through Lint (jsLint.com) is often revealing


 6:36 am on Oct 14, 2008 (gmt 0)

Krispy2, good link. I never used online services like that, it might be very helpful in some cases. After a few hours of coding I can turn a blind eye to some simple things like a "{" or ";"!

and, finally, my script and form both work fine!
big thanks to penders! DOM approach fixed it all. Very simple and precise suggestion!

updated script:

<script language="JavaScript">
var timer,x1,x2,x3,result;
function calculate()
if (x2 <= 10) {x2=500;}
else if (x2 > 10 && x2 < 50) {x2=1500;}
else {x2=3000;}

I really love this board! )

[edited by: Fokel at 6:37 am (utc) on Oct. 14, 2008]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved