alert not showing getElementbyId in javascript

mick picture mick · May 27, 2011 · Viewed 7.6k times · Source
function showPrice(){ 

    var a = document.getElementById("product_container15");

    if (a == "$1,599.00"){
        alert(a);
    }
    else {
        alert("$1,499.00");
    }
}

This is returning $1,499.00 all the time. I know I'm doing this wrong, or maybe there is a whole different way to write this. I want the alert to show $1,599.00 if the id is "product_container15". If it's not then the alert will show $1,499.00. Could someone show me how this is done? Thanks!

Answer

Oded picture Oded · May 27, 2011

a is an element - do you want the contents?

If this is HTML markup, try textContent or (for IE innerText):

alert(a.textContent);

Or the equivalent (for text nodes) nodeValue:

alert(a.nodeValue);

If a is a form element (input, textarea etc...), use the value property:

alert(a.value);