views:

78

answers:

5

I have a function

function setbid(catid)
{
if(catid == 30)
{

document.getElementById('bannerid').value = 1;

}
else if(catid == 31){
document.getElementById('bannerid').value = 2;
}
else if(catid == 32){
document.getElementById('bannerid').value = 6;
}
}

this function is being called onchange event of select box.

now when the value of select box is changed IE throws me an error saying

"document.getElementId(....)" is null or not an object..

Now what can i infer from this?

please help..

UPDATE

As per David's Reply how can i check for its existance before the assignment of value to that field,? Also other browsers do not display this error, I have this bannerid field as hidden field in the document... What should be done?

Below is the screen shot of the error displayed onchange of select box alt text

Update 2 Update as per the answer of @haylem

<script>
function setbid(catid)
{
var lookup = {
    '30' : 1,
    '31' : 2,
    '32' : 6
  };

  if (lookup.hasOwnProperty(catid)) {
    var el = document.getElementById('bannerid');

    if (el) {
      el.value = lookup[catid];
    }
  }
/*if(catid == 30)
{
if(typeof(document.getElementById('bannerid')) == null ){
document.getElementById('bannerid').value = 1;
}
}
else if(catid == 31){
document.getElementById('bannerid').value = 2;
}
else if(catid == 32){
document.getElementById('bannerid').value = 6;
}*/
}
</script>

<div style="margin:0px auto">
<div class="instantprice-popup-bg">
<form name="instantpop" id="instantpop" action="" method="post" style="margin:0px; padding:0px" onsubmit="">
    <input type="hidden" name="sessid" id="sessid" value="<?php echo $_GET['osCsid'];?>">
<div class="instantprice">

            <ul>
                <li class="text12pt" style="color:#FF0000; display:none;" id="appsize">For special low price on sizes over 30ft. wide and 10ft. height contact us on [email protected].</li>
                <li class="text12pt" style="color:#FF0000; display:none;" id="vinylsize">Please enter width upto 30 ft & height upto 5 ft for Vinyl Signs/Decals(Posters).</li>

                <li class="col1">Select Product</li>
                <li class="col2">

                    <input type="hidden" name="price" id="price" value="<?php echo "15"; ?>" />
                    <input type="hidden" name="bannerid" id="bannerid" value="" />
                    <select name="product" class="dropdownfield" id="product" style="width: 180px;" onchange="javascript:setbid(document.getElementById('product').value);">
                    <option value="">Select Product</option>
                    <?php
                        while($result=mysql_fetch_object($sql_query))
                        {
                            ?><option value="<?php echo $result->categories_id;?>" <?php if($_POST['product'] == $result->categories_id) {?> selected="selected" <?php } ?>><?php echo $result->categories_name;?></option><?php 
                        }
                    ?>
                    </select>

                </li>
                <li class="col1">Select Size (in ft.)</li>
                <li class="col2">
                    <div class="sizebox" style="width:250px;">
                        <div class="sizebox col1"><input name="size_w" id="size_w" type="text" class="sizetextfield1" value="<?php if(isset($_REQUEST['size_w'])){ echo $_REQUEST['size_w']; } else { echo "2"; } ?>" ></div>
                        <div class="sizebox col2"><img src="images/homepage/up-arrow.jpg" width="12" height="9" border="0" align="top" onclick="increaseCounter('size_w')" /><img src="images/homepage/down-arrow.jpg" width="12" height="9" border="0" onclick="decreaseCounter('size_w');" /></div>
                        <div class="sizebox col3"  style="width:35px;">width</div>
                        <div class="sizebox col1"><input name="size_h" id="size_h" type="text" class="sizetextfield1" value="<?php if(isset($_REQUEST['size_h'])){ echo $_REQUEST['size_h']; } else { echo "2"; } ?>" style="margin-left:8px" ></div>
                        <div class="sizebox col2"><img src="images/homepage/up-arrow.jpg" width="12" height="9" border="0" align="top" onclick="increaseCounter('size_h');" /><img src="images/homepage/down-arrow.jpg" width="12" height="9" border="0" onclick="decreaseCounter('size_h');" /></div>
                        <div class="sizebox col3" style="width:35px;">&nbsp;height</div>
                        <input name="size" id="size" type="hidden" value="<?php echo $_REQUEST['size_h'].'x'.$_REQUEST['size_w']; ?>">
                    </div>
                </li>
                <li class="col1">Select Quantity</li>
                <li class="col2">
                    <select id="qty" name="qty" >
                        <?php 
                            for($i=1;$i<=100;$i++)
                            {
                                ?> <option value="<?php echo $i;?>" <?php if($_POST['qty'] == $i) {?> selected="selected" <?php } ?>><?php echo $i;?></option> <?php
                            }
                        ?>
                    </select>
                </li>
                <li class="col1">Select Shipping</li>
                <li class="col2">
                    <select id="shipping_method" name="shipping_method" class="dropdownfield" >
                        <option value="">Select Shipping</option>
                        <option value="standard" <?php if($_POST['shipping_method'] == 'standard') {?> selected="selected" <?php } ?>>Standard (7 - 12 days from proof)</option>
                        <option value="expedited"<?php if($_POST['shipping_method'] == 'expedited') {?> selected="selected" <?php } ?>>Expedited (5 work days from Proof)</option>
                    </select>
                </li>
                <li class="col1" id="sidehead">Two Side Banner</li>
                <li class="col2" id="sideval">
                    <input type="checkbox" id="side" name="side" value="1"  />
                </li>
                <li class="col1">Product Price</li>
                <li class="col2"><span class="text15pt" id="total_display">$0</span>
                    <input type="hidden" id="total" name="total" class="textfield" value="15" readonly="readonly" style="width:60px;"/>
                </li>

                <li class="col1">Shipping Cost</li>
                <li class="col2"><span class="text15pt" id="shipping_display">$0</span>
                    <input type="hidden" id="shipping" name="shipping" class="textfield" value="" readonly="readonly" style="width:60px;" />
                </li>
                <li class="bannerprice" id="grand_total_display">TOTAL PRICE : $0

                </li><input type="hidden" id="grand_total" name="grand_total" value="" readonly="readonly" style="width:60px;" />
                <li style="float:none; text-align:center">

                    <input type="submit" class="getprice-btn" name="getprice" value="" />

                </li>
                <li class="text12pt">Final Price, No Hidden Charges</li>
            </ul>
            <input type="hidden" name="getpriceclick" value="1" />
        </div>
        </form>
</div>
</div>
+5  A: 

Now what can i infer from this?

That there isn't an element with the id bannerid in the document.

David Dorward
So how can i check for its existance before the assignment of value to that field,? Also other browsers do not display this error, I have this bannerid field as hidden field in the document... What should be done?
OM The Eternity
`b = document.getElementById('bannerid');if(typeof b =='object'){b.value =n;}`
Kristoffer S Hansen
It still throws me the error
OM The Eternity
@David pls help me to get into the issue
OM The Eternity
Kristoffer has already told you how to do that. Possibly your description of the problem has mistakes in it.
David Dorward
A: 

You just enable that hidden field and check whether that field has any value while browser's throws an error.

Mohan
What do u mean by enabling the hidden field?
OM The Eternity
Instead of using hidden field u plz try text field and check that has a value or not. This is not a solution, trying this u may get clear idea.
Mohan
I did it and it still throws me the same error
OM The Eternity
+2  A: 

Rewrite as:

function setbid(catid) {
  var lookup = {
    '30' : 1,
    '31' : 2,
    '32' : 6
  };

  if (lookup.hasOwnProperty(catid)) {
    var el = document.getElementById('bannerid');

    if (el) {
      el.value = lookup[catid];
    }
  }
}

And make sure that there is an element with the id attribute exactly written as bannerid.

The function I give you is slightly different than yours:

  • using a lookup table to get values instead of if chains (especially if you will add values later)
  • doing the element lookup in only one part of the code instead of duplicating it
  • checking for the element's existence

EDIT for Update2:

Note directly relevant to your problem, but:

  • You should rather but your script tag in your <head> section
  • You should rather use progressive enhancement to react to events instead of mangling HTML and JavaScript code. Use event hooks instead.

Also check your the rendered page's source code to see if some elements were not eaten by the renderer because of a a malformation, or by your PHP code.

EDIT:

If I test with the following, it works perfectly fine:

<script>
function setbid(catid)
{
var lookup = {
    '30' : 1,
    '31' : 2,
    '32' : 6
  };

  if (lookup.hasOwnProperty(catid)) {
    var el = document.getElementById('bannerid');

    if (el) {
      el.value = lookup[catid];
      alert('done [' + el.value + ']');
    }
  }

}
</script>

<div style="margin:0px auto">
<div class="instantprice-popup-bg">
<form name="instantpop" id="instantpop" action="" method="post" style="margin:0px; padding:0px" onsubmit="">
    <input type="hidden" name="sessid" id="sessid" value="<?php echo $_GET['osCsid'];?>">
<div class="instantprice">

            <ul>
                <li class="text12pt" style="color:#FF0000; display:none;" id="appsize">For special low price on sizes over 30ft. wide and 10ft. height contact us on [email protected].</li>
                <li class="text12pt" style="color:#FF0000; display:none;" id="vinylsize">Please enter width upto 30 ft & height upto 5 ft for Vinyl Signs/Decals(Posters).</li>

                <li class="col1">Select Product</li>
                <li class="col2">

                    <input type="hidden" name="price" id="price" value="15" />
                    <input type="hidden" name="bannerid" id="bannerid" value="" />
                    <select name="product" class="dropdownfield" id="product" style="width: 180px;" onchange="javascript:setbid(document.getElementById('product').value);">
                    <option value="">Select Product</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="30">30</option>
                    <option value="31">31</option>
                    <option value="32">32</option>
                    </select></li>
            </ul>
            <input type="hidden" name="getpriceclick" value="1" />
        </div>
        </form>
</div>
</div>

So you have something screwing up your elements. I'd suspect something going wrong in your PHP code and producing a malformed output.

haylem
Thanks Heylem, I tried ur way But it still throws me error, error displayed has been added as screen shot above...
OM The Eternity
@OM The Eternity: OK. Are you sure it comes from that piece of code? And where do you put your code in your page? If you provided us with a more complete code snippet, we might be able to help more.
haylem
Chk the Updated Question for code
OM The Eternity
@OM The Eternity: As far as I can see the code seems fine. Can you post the full-page's code? Are elements really in this order (position of the script tag in the page, etc...)?
haylem
@OM The Eternity: also, instead of the original source code, check the generated source as rendered in your browser see if the bannerid element is really here, as maybe you have a PHP error that prevent your element to be displayed and part of your code is not printed to the page.
haylem
@Haylem thanx for the analysis , I have already checked the source code And I found the bannerid field in it, yes the form is as it is displayed above, I have just ommited the Mysql queries for the safety reasons
OM The Eternity
@OM The Eternity: check my updated answer. So either something is wrong in your code and the output gets messed up OR the error is displayed by another piece of JavaScript.
haylem
I have attempted all the stuffs a but i still get the error at the end, my bannerid field get desirable assigned value but as the script is ended the same error box is displayed.. i am facing this issue only in IE6 and IE7
OM The Eternity
@OM The Eternity: if the value get set, then that document.getElementById error comes from somewhere else. You will have to use debug statements and try to figure out where it is. There's obviously some other JS code being run on your page.
haylem
+1  A: 

If the element does not exist on the DOM when this function is executed, the document.getElementById(...) will return null (or undefined).

Always best to use a script inspector (like FireBug or Safari console) while developing. If you must develop in IE6, you might want to inspect by incrementally "alert"ing.

Suggested debug output:

alert(document.getElementById);
alert(document.getElementById("bannerid"));
alert(document.getElementById("bannerid").value);
Oxyrubber
Also, testing existence of elements can't be done against typeof(...) === "object" because... typeof(null) === "object". It's a controversial design decision, but the best way to avoid this is to test against null: if(typeof(el) !== null) {el.value = "foo";}
Oxyrubber
A: 
 <script type="text/javascript">
function notEmpty(){

    var myTextField = document.getElementById('myText');

    if(myTextField.value != "")
      {
        alert("You entered: " + myTextField.value)
            alert(document.getElementById('myText1').value=0);
      }
    else
        alert("Would you please enter some text?")      
}
</script>
<input type='text' id='myText' />
<input type='text' id='myText1' />
<input type='button' onclick='notEmpty()' value='click' />

Try this example

Mohan
To debug the javascript function, just put the following statement as the first line of the function:alert("[" + bannerid + "]"); return false;This will display a bannerid value. If two empty brackets appear [] then you know the value passed was blank.
Mohan
function setbid(catid){alert("[" + bannerid + "]");if(catid == 30){alert(document.getElementById('bannerid').value = 1);}else if(catid == 31){alert(document.getElementById('bannerid').value = 2);}else if(catid == 32){(document.getElementById('bannerid').value = 6);}
Mohan
its works fine for me in firefox.
Mohan
Yes It does work fine in FF problem is with IE6 and IE7
OM The Eternity