PDA

View Full Version : ElementById Not a function? Huh?



crono.Serge
Apr 27th, 2007, 11:19 PM
Hi everybody! Sorry to ask for help right before a weekend, and I hope all enjoy there's, but, I'm confused as to why FF Error console is giving me the fin..I mean, document.ElementById is not a function. What I coded seems correct, but I just can't figure out why this is happening?



function enableFields()
{
if (document.getElementById("chkYes").checked == true && document.getElementById("chkNo").checked == false)
{
document.getElementById("txtAdd_Disp").disabled = false;
document.getElementById("txtRem_Disp").disabled = true;
document.getElementById("txtRem_Disp").value = "";
document.getElementById("iStock").value = document.GetElementById("txtAdd_Disp").value + document.GetElementById("sOnHand").value;
}
else if (document.getElementById("chkYes").checked == false && document.getElementById("chkNo").checked == true)
{
document.getElementById("txtAdd_Disp").disabled = true;
document.getElementById("txtRem_Disp").disabled = false;
document.getElementById("txtAdd_Disp").value = "";
document.getElementById("iStock").value = document.GetElementById("sOnHand").value - document.GetElementById("txtRem").value;
}
}

The bold part is where it throws up. Any idea of what I did wrong?

david_kw
Apr 27th, 2007, 11:31 PM
Take a close look at the line you highlighted and you'll see that "Get" is capitalized in the second call like

document.GetElementById()

but it needs to be

document.getElementById()

like it was the first time. JavaScript is case sensitive so it really does make a difference. You have to change all the lines that have the wrong case (I think I see at least 4 incorrect calls).

david_kw

crono.Serge
Apr 27th, 2007, 11:39 PM
Oh, ok. I'll change that, but, I also have another question.

If wanted to check if there is any information within a given field (input box), would I use the length method or just a .value at the end of the statement?
.........

Or am I WAY off?

david_kw
Apr 28th, 2007, 02:00 AM
I believe if you compare it to an empty string like "" is the standard way.

so

if (document.getElementById('myid').value == "") {
alert("You can't have an empty text box.");
}

Something like that.

david_kw

liorean
Apr 28th, 2007, 02:07 AM
If wanted to check if there is any information within a given field (input box), would I use the length method or just a .value at the end of the statement?If you want to check only whether it's empty or not, you can use either
if(elm.value!=''){
doSomethingIfFieldIsNonEmpty();
}or
if(elm.value.length>0){
doSomethingIfFieldIsNonEmpty();
}

Or you can do string or regex comparisons on the value if you want to confirm the input matches some format.