/    Sign up×
Community /Pin to ProfileBookmark

I get error: null or not an object

I’m new to Javascript and I can’t figure out why this is giving me an error. Is it about the code or something about the browser?

[QUOTE]

<body>
<font face=”Helvetica, Arial” size=”2″ color=”#000000″>

Season
<select name=”season” size=”1″ onChange=”calcweight()”>
<option selected >Ban season
<option >Post ban

</select><br>

</font>

<table border=”0″>
<font face=”Helvetica, Arial” size=”1″ color=”#000000″><strong>
<tr>
<td align=”top” width=”100″> Axle Group </td>
<td align=”top” width=”50″> Number of axles </td>

<td align=”top” width=”80″> Tire Capacity </td>
<td align=”top” width=”80″> weight </td>

</tr>

<tr>
</tr>
<tr>
<td>carriers</td>

<td> <select name=”axle” size=”1″ onChange=”calcweight()”>
<option selected >single
<option >tandem
</select> </td>
<td> </td>

<td> <input type=”text” name=”rated” value=”0000″ size=”8″ onKeyUp=”calcweight()” > </td>

<td> <input type=”text” name=”weight” size=”8″ onKeyUp=”calcweight()”> </td>

</tr>
<table>
<tr>
<td width=”160″> <input type=”button” value=”Calculate Weight” onClick=”calcweight()”> </td>
</tr>
</table>
</body>
<script language = “JavaScript”>
<!– hide from non-javascript browsers

function calcweight()

{
var axle = ‘single’;
var axle = ‘tandem’;
var season = ‘Ban season’;
var season = ‘Post ban’;

if (axle == ‘single’) {

if (season == ‘Ban season’) {axl = 9100; }
if (season == ‘Post ban’) {axl = 10000; }

document.write.weight.value=axl+ ” kg. ”
}

if (axle == ‘tandem’) {

if (season == ‘Ban season’) {axl = 17000; }
if (season == ‘Post ban’) {axl = 19000; }

document.write.weight.value=axl+ ” kg. ”
}

}

[/QUOTE]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@rpgivpgmrJun 29.2007 — B&B,

There were a few problems. Some of which can be seen more easily by maintaining formatting, some by referring to the document object. Here is

a version you can play with that may help out:

<html>

<head>

<script language = "JavaScript">

<!-- // hide from non-javascript browsers

function calcweight() {

var axl = '';

// some validation
if (document.frm.axle.value == 'single') {
switch (document.frm.season.value) {
case 'Ban season':
axl = '9100';
break;
case 'Post ban':
axl = '10000';
break;
}

} else {
switch (document.frm.season.value) {
case 'Ban season':
axl = '17000';
break;
case 'Post ban':
axl = '19000';
break;
}
}
document.frm.weight.value = axl + " kg.";
}

function IsNumeric(strString) {
// check for valid numeric strings
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1) {
blnResult = false;
}
}
return true;

}
//-->
</script>
<style>
.td {
font face="Helvetica, Arial";
size="1";
color="#000000";
}
</style>

</head>

<body>

<form name="frm">

<font face="Helvetica, Arial" size="2" color="#000000">

SEASON&nbsp;&nbsp;
<select name="season" size="1" onChange="javascript:calcweight();">
<option selected value='Ban season'>Ban season</option>
<option value='Post ban'>Post ban</option>
</select><br>
</font>
<table width="100%" cellpadding=0 cellspacing=0 border=0>
<tr>
<td align="top" width="20%">AXLE GROUP</td>
<td align="top" width="10%">Number of axles</td>
<td align="top" width="5%">&nbsp;</td>
<td align="top" width="10%">Tire Capacity </td>
<td align="top" width="10%">Weight</td>
<td align="top" width="55%">&nbsp;</td>
</tr>
<tr><td colspan=6>&nbsp;</td></tr>
<tr>
<td>CARRIERS</td>
<td>
<select name="axle" size="1" onChange="javascript:calcweight()">
<option selected value='single'>single</option>
<option value='tandem'>tandem</option>
</select>
</td>
<td>&nbsp;</td>
<td><input type="text" name="rated" value="0000" size="8" onKeyUp="javascript:calcweight();" > </td>
<td><input type="text" name="weight" size="8" onKeyUp="javascript:calcweight();"></td>
</tr>
<tr><td colspan=6>&nbsp;</td></tr>
<tr><td colspan=6 align=center><input type="button" value="Calculate Weight" onclick="javascript:calcweight();">&nbsp;<input type="reset" value="Reset"></td></tr>
</table>
</form>

</body>

</html>

You might just take the calculate button off the form after you get it adjusted the way you want it since it will never get clicked anyway. Notice I added the form object, used the switch structure instead of multiple if statements, and added values to the select options. I stuck an isnumeric function in there in case you want to validate the user's rated input. Just do a if (!IsNumeric(document.frm.rated.value)) { }, then if you want to test for zero just do a var r = parseInt(document.frm.rated.value); but only after isnumeric check above. Hope this helped a little.
Copy linkTweet thisAlerts:
@rootJun 29.2007 — I( keep seeing this type of use of javascript
&lt;select name="axle" size="1" onChange="javascript:calcweight()"&gt;
You do not use javascript: in an event that is calling a script, these events are anonymous functions in any case and do not need the javascript: prefix.
Copy linkTweet thisAlerts:
@rpgivpgmrJun 29.2007 — .,

I think you are right. Old habits die hard. I think it used to be necessary sometime during the great 'Javascript/JScript' Wars, which JScript lost, and are still being fought on a minor level in the .net framework. LOL
Copy linkTweet thisAlerts:
@B_amp_BauthorJun 29.2007 — Thanks for the help guys?

I tried using switch statements at first but I got confused so I tried to just use if statements instead but now I can finally get on with it.
Copy linkTweet thisAlerts:
@rpgivpgmrJun 29.2007 — B&B,

You are doing fine. Just stay at it. It will get easier as you go along.

The easiest issue to forget with switch statements is that as all Javascript,

they are case sensitive! Plus forgetting the colon. Just keep a snippet of the structure somewhere you can reference and you don't have to remember it all the time. That's what I do. Here is the snippet I keep:

//--begin snippet

switch (expression) {

case value1:

statement;

break;

case value2:

statement;

break;

default : statement;

}

// REMEMBER LOWER CASE KEYWORDS!

//--end snippet
Copy linkTweet thisAlerts:
@B_amp_BauthorJun 29.2007 — Thanks!

I have another question. I've been trying to embed another if statement before the switch statement. Is that possible? I'm trying to account for another dropdown menu: Tire Diameter

function calcweight() {

var axl = '';

if (document.frm.tirediameter.value == '12 in.') {

if (document.frm.axle.value == 'Four') {

switch (document.frm.season.value) {

case 'Fall':

axl = '8050';

break;

case 'Winter':

axl = '8050';

break;

}

}

else {

switch (document.frm.season.value)

case 'Fall':

axl = '7920';

break;

case 'Winter':

axl = '7920';

break;

}

}

else {

if (document.frm.axle.value == 'Four') {
switch (document.frm.season.value) {;
case 'Fall':
axl = '9412';
break;
case 'Winter':
axl = '12500';
break;

}

}

else {

switch (document.frm.season.value)

case 'Fall':

axl = '9260';

break;

case 'Winter':

axl = '12500';

break;

}

}

document.frm.weight.value = axl + " kg.";

}

.

.

.

.

<table width="100%" cellpadding=0 cellspacing=0 border=0>

<tr>

<td align="top" width="20%">Axle Group</td>

<td align="top" width="10%">Number of axles</td>

<td align="top" width="1%">&nbsp;</td>

<td align="top" width="10%">Tire Diameter</td>

<td align="top" width="10%">Tire Capacity </td>

<td align="top" width="10%">Weight</td>

<td align="top" width="55%">&nbsp;</td>

</tr>

<tr><td colspan=6>&nbsp;</td></tr>

<td>Carrier A</td>

<td>

<select name="axle" size="1" onChange="javascript:calcweight()">

<option selected value='Four'>Four</option>

<option value='Five'>Five</option>

</select>

</td>

<td><select name="tirediameter" size="1" onChange="javascript:calcweight()">

<option selected value='12 in.'>12 in.</option>

<option value='13 in.'>13 in.</option>

</select>

</td>

<td>&nbsp;</td>

<td><input type="text" name="rated" value="0000" size="8" onKeyUp="javascript:calcweight();" > </td>

<td><input type="text" name="weight" size="8" onKeyUp="javascript:calcweight();"></td>

<td><input type="text" name ="note" value=" " size="14" ></td>

</tr>

<tr><td colspan=6>&nbsp;</td></tr>

<tr><td colspan=6 align=center><input type="button" value="Calculate Weight" onclick="javascript:calcweight();">&nbsp;<input type="reset" value="Reset"></td></tr>

</table>
[/QUOTE]
Copy linkTweet thisAlerts:
@rpgivpgmrJul 01.2007 — Just wrap the code you want inside of your if statement,

(i.e. save a copy of the page first and work on another copy):


if (document.frm.fieldname.value == 'fieldvalue') {



and




}


God Bless,

Don
Copy linkTweet thisAlerts:
@B_amp_BauthorJul 03.2007 — Thanks! It was just a matter of placing the curly braces {} in the right place.

But now if I have two if statements and each produces a different number and I want to add the 2 numbers within the function calweight (). It may sound simple but I can't figure it out.
Copy linkTweet thisAlerts:
@rpgivpgmrJul 03.2007 — Just place each in a variable and have a third statement add the two variables.

var a = ...

var b = ...

var c = a + b;
Copy linkTweet thisAlerts:
@B_amp_BauthorJul 03.2007 — Great! I have it working, thanks for your help. I do apreciate it
×

Success!

Help @B_amp_B 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.5,
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,
)...