Daniel Afonso
Daniel Afonso

Reputation: 30

JavaScript Uncaught TypeError?

Can anyone halpe me with this error:

Uncaught TypeError: Cannot read
property 'length' of undefined

website

When you change the values from combo it should change the price below, but it is not working

function update_price_select2(o, element_name)
{   

    var sel_obj = null;
    var element = null;
    //price_original = <?php //echo substr($products_price,0,-1); ?>;
    //alert("Preço Original:" + price_original);
    if (document.getElementById)  // DOM3 = IE5, NS6
    {  
        sel_obj = document.getElementById(o);
         //alert("sel_obj "+ sel_obj);
        element = document.getElementById(element_name);
         //alert("element "+ element);
    }
    //alert(o);
    //alert(element_name);
    var index = sel_obj.selectedIndex;
    //alert(o+" index");
    var price_array = option_price[sel_obj.id];
    var price = price_array[index];
    var price_for_operation = price.substr(0,price.length -1); //the price from the option choosen

    if (price_for_operation == '')
    {
        price_for_operation = 0;
    }

    //Ao preço original, vamos retirar o preço da variável que este tinha anteriormente.
    current_product_price = current_product_price - option_price[sel_obj.id + '_current'];
    //Vamos colocar o valor actual da variável seleccionada 
    option_price[sel_obj.id + '_current'] = price_for_operation;

    if (price_for_operation != 0)
    {
        var final_price = roundNumber(parseFloat(current_product_price) + parseFloat(price_for_operation),2); 
        final_price = final_price.toFixed(2);
    }
    else
    {
        var final_price = current_product_price.toFixed(2);
    }
    //Nos save the decimal value, without the € or $, so we can use it in the next options call
    current_product_price = final_price;
    final_price = final_price + money_simbol;  

    if (final_price != "")
    {    
        display_updated_price(final_price, element);
        update_allowbuy(final_price);
    }

}

Upvotes: 0

Views: 8991

Answers (2)

sled
sled

Reputation: 14625

can you post the line where the error occurs? Your error tells me that you try to access the property length of an object which couldn't be found.

I get a smiliar error when I visit your page:

var order = document.getElementById('".BUTTON_IN_CART."'); 
order.style.visibility = 'visible';` 

Telling me that "order" is undefined, this is because it can't find the variable BUTTON_IN_CART and therefore it won't get an element! You should always check if the element you're looking for exists.

Edit:

Extend your code to check that the element is found:

if (document.getElementById)  // DOM3 = IE5, NS6
{  
    sel_obj = document.getElementById(o);
     //alert("sel_obj "+ sel_obj);
    element = document.getElementById(element_name);
     //alert("element "+ element);
}

if(sel_obj == null) { alert("Sell Obj not found!"); }
if(element == null) { alert("Element not found!"); }

Upvotes: 0

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

Check if your "price" is valid value. If "price" is undefined, then you cannot use the line "price.length" as in your code. My guess would be that your price is surely undefined.

Upvotes: 2

Related Questions