Reply
el has no properties error
Old 03-29-2008, 08:05 PM el has no properties error
Novice Talker

Posts: 9
Name: Bob
Hi,

I know this is very simple but I'm stuck!

I keep getting an "el has no properties" error. What am I doing wrong?

This is for an auction web site where the user selects an item in the list and then enters an numeric value/amount, and depending on the user input, there are two boxes towards the end of the page that shows how much the item he selected costs plus the total value/fees.

Code:
<script language="javascript">

function change_content(obj, num) {
if (document.getElementById) {el = document.getElementById(obj) ;}
else if (document.all){    el = document.all[obj]; }
else if (document.layers) {el = document.layers[obj];}
el.innerHTML = "<b>$"+num+"</b>";  <------ This is the line with the error
}

function changenow(){
el1 = document.forms["myform"].elements["VALUE1"].value;
el2 = document.forms["myform"].elements["VALUE2"].value;
 
show2 = el1*el2;
show = show2*FLATFEE; 

show2 = show2+show;
show = Math.round(show*100)/100;
show2 = Math.round(show2*100)/100;
 
change_content('FLATFEE',show);
 change_content('TOTALCOST',show2); 

}

</script>
Please note the caps like FLATFEE, TOTAL COST represent numeric values.

And towards the end of the page is the html part:

This is where the user is making his selection.
Code:
<select onChange="javascript:changenow();" name="VALUE2" style="width:20%" />
<option value="1">1</option>
<option value="2">2</option>
and here he enters a numeric value
<input onChange="javascript:changenow();" name="VALUE1" type="text">
[/code]

and finally the outcome(s)

Outcome 1
Code:
<div id='FLATFEE'></div></font>
<script language="javascript">
changenow();
</script>
Outcome 2
Code:
<div id='TOTALCOST'></div></font>
<script language="javascript">
changenow();
</script>
Any other thoughts or ideas would be greatly appreciated.....Thanks in advance! :thumbsup:
bob25 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-30-2008, 06:13 AM Re: el has no properties error
chrishirst's Avatar
Super Moderator

Posts: 13,630
Location: Blackpool. UK
Quote:
Please note the caps like FLATFEE, TOTAL COST represent numeric values
element IDs cannot start with a number
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-30-2008, 07:36 AM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
Thanks for the fast response. My mistake, I didn't express it right... I mean, the final outcome -what end users see- is numeric values. The FLATFEE, TOTAL COST etc. are exactly like this.
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 07:39 AM Re: el has no properties error
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Like this, I don't see any problem in your code.
Is this problem arising in firefox, IE, safari or another browser ?

Quote:
element IDs cannot start with a number
Chris is right, you cannot name an id with a numeric value, it has to begin with a to z, A to Z or _ character.
Put a prefix on them, it may solve your problem.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 03-30-2008, 07:46 AM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
Guys, thank you very much for your help.

The javascript code is part of an auction script (custom made) for a local charity (they will auction some drawings made by kids) and my javascript knowledge is very limited, compared to you.

I get the same error both in IE (debugger) and Firefox (error console):

"El has no properties"

What exactly do I have to edit now regarding the numeric values you both mentioned above? I'm kind of lost...


PS: Talkputation added!

Last edited by bob25 : 03-30-2008 at 07:54 AM. Reason: Talkputation left
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 08:15 AM Re: el has no properties error
chrishirst's Avatar
Super Moderator

Posts: 13,630
Location: Blackpool. UK
lets see the live page and we'll probably be of more help
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-30-2008, 08:24 AM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
Nothing is up yet, because of this error, which make the site look pretty unprofessional. Sorry about that...

When you say that element ids should not have numeric values, what exactly do you mean? And, if possible, what do I have to edit? I'm willing to experiment with this on my own, as long as someone can point me to the right direction.
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 08:34 AM Re: el has no properties error
chrishirst's Avatar
Super Moderator

Posts: 13,630
Location: Blackpool. UK
Copy and pasting the entire page source from your test environment will do then .

http://www.w3.org/TR/html401/types.html#type-name

so "1element" or "123" as a name or ID is illegal usage.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-30-2008, 08:34 AM Re: el has no properties error
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Quote:
element ids should not have numeric values
No.
an id="34567" is incorrect.
An id should start with a letter or _, so using id="val_34567" would be correct.

Could you post here the complete page ?
The error may come from the structure of the html. it would be easier for us to spot the problem if we can have a full view on the problem-
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 03-30-2008, 09:59 AM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
Guys, I REALLY appreciate your help! I have only removed the sql query values. The rest of the stuff, is exactly the same:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<title>Testing Auction-Place Your Bid</title>
</head>

<body>

<script language="javascript">

function change_content(obj, num) {

if (document.getElementById) {el = document.getElementById(obj) ;}
else if (document.all){    el = document.all[obj]; }
else if (document.layers) {el = document.layers[obj];}
el.innerHTML = "<b>$"+num+"</b>";//
}



function changenow(){
 el1 = document.forms["form"].elements["bid"].value;
 el2 = document.forms["form"].elements["quantity"].value;
 
 show2 = el1*el2;
 
 show = show2*<?=$join_auction_fee?>+<?=$flatfee?>; 
 
 
 
 
 show2 = show2+show;
  
 show =Math.round(show*100)/100;
 show2 =Math.round(show2*100)/100;
 
 change_content('join_auction_fee',show);
 change_content('totalcost',show2); 

}

</script>


<?php
if ((!is_numeric($bid)) or ($bid<$minimum_price)){ $error.="You have entered an incorrect amount."; }
if ((!is_numeric($quantity)) or ($quantity>$maximum_quantity)){ $error.="Sorry, but you are not allowed to order so many copies for the same drawing."; }
if ($error){
errormessage($error);
} else {
mysql_query("INSERT INTO bids (...) VALUES('...')") or die(mysql_error());
successmessage("Thank you for your contribution!");
}
?>


<form name="form" method="post" action="index.php">

<table width=100% align=center cellpadding=1 cellspacing=1>
<tr align=left>
<td>Quantity:</td>
<td>
<select onChange="javascript:changenow();" name="Quantity">
<option value="1">1</option>
<option value="2">2</option>
</select>    
</td>
</tr>

<tr align=left>
<td>Your bid amount:</td>
<td><input onkeyup="javascript:changenow();" name="bid" maxlength=3 type="text"></td>
</tr><tr align=left>
<td>Comments:</td>
<td><textarea style='width:150px;' name="comments"></textarea></td>
</tr></table>

<table width=100% align=center cellpadding=1 cellspacing=1>
<tr>
<td align=center>Flat fee:</td>
<td>
<div id='join_auction_fee'></div>
<script language="javascript">
changenow();
</script>    
</td>

</tr><tr>

<td>Total Cost:</td>
<td>
<div id='totalcost'></div>
<script language="javascript">
changenow();
</script>
</td>
</tr></table>

<br /><br />
<div align=center>
<input type="submit" name="placebid" value="Place your Bid!">
</div>
</form>

</BODY>
</HTML>
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 11:01 AM Re: el has no properties error
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
ok.
first:
Code:
document.forms["form"].elements["quantity"].value;
is invalid for a select element. IE support it, but it's not the correct way to get the value back.
Code:
var selElm=document.forms['form'].Quantity;
var el2=selElm.options[selElm.selectedIndex].value;
is the right way to go.

Second: You have included 2 times the section
Code:
<script language="javascript">
changenow();
</script>
So, you do 2 times all the operations.
Your error comes from the fact that the browser interpret javascript WHEN BUILDING THE PAGE. So, as the call is made after the declaration of "div id=join_auction_fee", it passes this without a hitch.

But, "div id=totalcost" has still not been rendered, so it don't find it.
Moving the changenow() at the end of the page, it will find the element, and solve your problem.

Third:
Normally, a variable in javascript has to be initialized with var {variable}={value or null}
Some browser can react in strange ways depending of the quircks mode is active or not if you don't declare them.

Too, even if you search for something, consider that you won't always find it.
Before processing it, test if to see if you have found it
Code:
var el=false;
    if (document.getElementById) {
      el = document.getElementById(obj) ;
    }
    else if (document.all){
      el = document.all[obj]; 
    }
    else if (document.layers){
      el = document.layers[obj];
    }
    
    if(el!==false){
     el.innerHTML = "<b>$"+num+"</b>";//
    }
    else{
     alert('Target element '+obj+' not found.');
    }
In the same optic, don't blindly trust what the users gives you as an input.
Code:
var el1=document.forms["form"].elements["bid"].value;
...
var show2 = el1*el2;
What if the user don't enter a numeric value in the text field ?
Force the computing to get a numeric value with parseFloat(string)
Code:
var el1=parseFloat(document.forms["form"].elements["bid"].value);
...
var show2 = el1*el2;
This will ensure that the value of el1 is always a numeric or null value.

So you know, <script language=> is deprecated.
You should use
Code:
<script type="text/javascript">
And last thing, I have replaced the php variables with plain numbers for my tests, but take care of the operator order:
Code:
var show = show2*2+3;
this will be interpreted as
Code:
var show = (show2*2)+3;
and not
Code:
var show = show2*(2+3);
because like in "real" mathematics, the division and multiplication operators have a greater precedence over addition and substraction.



This is the file I ended up with:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Testing Auction-Place Your Bid</title>
</head>

<body>

<script type="text/javascript">

function change_content(obj, num) {
    var el=false;
    if (document.getElementById) {
      el = document.getElementById(obj) ;
    }
    else if (document.all){
      el = document.all[obj]; 
    }
    else if (document.layers){
      el = document.layers[obj];
    }
    
    if(el!==false){
     el.innerHTML = "<b>$"+num+"</b>";//
    }
    else{
     alert('Target element '+obj+' not found.');
    }
}



function changenow(){
 var el1 = document.forms["form"].elements["bid"].value;
 var selElm=document.forms['form'].Quantity;
 var el2=selElm.options[selElm.selectedIndex].value; 
 var show2 = el1*el2;
 var show = show2*2+3; 
 
 show2 = show2+show;
 show =Math.round(show*100)/100;
 show2 =Math.round(show2*100)/100;
 
 change_content('join_auction_fee',show);
 change_content('totalcost',show2); 
}

</script>

<?php
if ((!is_numeric($bid)) or ($bid<$minimum_price)){ $error.="You have entered an incorrect amount."; }
if ((!is_numeric($quantity)) or ($quantity>$maximum_quantity)){ $error.="Sorry, but you are not allowed to order so many copies for the same drawing."; }
if ($error){
errormessage($error);
} else {
mysql_query("INSERT INTO bids (...) VALUES('...')") or die(mysql_error());
successmessage("Thank you for your contribution!");
}
?>


<form name="form" method="post" action="index.php">

<table width=100% align=center cellpadding=1 cellspacing=1>
    <tr align=left>
        <td>Quantity:</td>
        <td><select onChange="javascript:changenow();" name="Quantity">
            <option value="1">1</option>
            <option value="2">2</option>
        </select></td>
    </tr>

    <tr align=left>
        <td>Your bid amount:</td>
        <td><input onkeyup="javascript:changenow();" name="bid" maxlength=3 type="text"></td>
    </tr>
    <tr align=left>
        <td>Comments:</td>
        <td><textarea style='width: 150px;' name="comments"></textarea></td>
    </tr>
</table>

<table width=100% align=center cellpadding=1 cellspacing=1>
    <tr>
        <td align=center>Flat fee:</td>
        <td>
          <div id='join_auction_fee'></div>
        </td>
    </tr>
    <tr>
        <td>Total Cost:</td>
        <td>
          <div id='totalcost'></div>
        </td>
    </tr>
</table>
<br />
<br />
<div align=center><input type="submit" name="placebid" value="Place your Bid!"></div>
</form>
<script language="javascript">
  changenow();
</script>
</BODY>
</HTML>
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 03-30-2008 at 11:36 AM.
tripy is online now
Reply With Quote
View Public Profile
 
Old 03-30-2008, 11:35 AM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
I can't believe it... it works! Thanks a ton tripy! You are the best!!!
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 11:43 AM Re: el has no properties error
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
no sweat, but I kept editing my post, adding remarks until you posted that reply.
I encourage you to read my last reply completely, there are some simple thing you should take care of (like the parseFloat() call) to strengthen your code.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 03-30-2008, 02:17 PM Re: el has no properties error
Novice Talker

Posts: 9
Name: Bob
Woa! Thank you so much tripy.

Regarding the user input: Should I use the Javascript parseFloat or is the user input already being taken care of with the existing PHP code? Or should I use both? (i.e. for extra security)

Code:
if ((!is_numeric($bid)) {....error-message....
if ((!is_numeric($quantity)) {...error-message....
Did I understand correctly? Is this what you're saying?

Many thanks once more! I really appreciate it.
bob25 is offline
Reply With Quote
View Public Profile
 
Old 03-30-2008, 02:28 PM Re: el has no properties error
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
Javascript is a client side technology.
A power user could craft the form value, bypassing your javascript.

So, as much as possible, keep an check in PHP.
Javascript side checks are "nice to have", but server side checks are a necessity.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile