Link to home
Create AccountLog in
Avatar of burnsj2
burnsj2

asked on

Javscript text box value problem with getElementById()

I have the following code in the document head:

<script language="JavaScript">
     function uploadAll(){                  
            //check for non blank or default value
            var y=document.getElementById('newAlbumName');
            if(y.value=="" || y.value=="Enter Album Name"){
                  alert("Album Name Required yval="+y.value);
                  return false;
            }
      }

</script>

And in the body:

<form>
<input id="newAlbumName" type=text name='newAlbumName' value="Enter Album Name">
<input type="button" value="UploadAll" onClick="uploadAll();">
</form>

When I enter "1234" into the text box and click the UploadAll button, the alert says "Album Name Required yval=".  It seems that y.value is NULL, why is this?  How can I get the value of the text box to be in the alert, ie. "Album Name Required yval=1234"

Thanks.
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

You have to move the alert outside of the if.

     function uploadAll(){                  
            //check for non blank or default value
            var y=document.getElementById('newAlbumName');
            alert("Album Name Required yval=" + y.value);
            if(y.value=="" || y.value=="Enter Album Name"){
                  alert("Album Name Required yval="+y.value);
                  return false;
            }
      }

That first alert will always appear, even if 1234 is the value.  In the second case the alert will only show if the value is blank or that default phrase.

Let me know if you have a question or need more info.

bol
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of burnsj2
burnsj2

ASKER

b0lsc0tt: the second alert is always appearing because the y.value is always NULL
What Javascript error are you getting then?  What does the rest of the html and script look like?  That input element is the only one with an id of newAlbumName, right?

Using the form, like suggested above, is another way to do this but it seems like you have a bigger problem here.  I don't see a reason, in the snippet you have provided, for the value not being available.  There is certainly no reason for it to be Null.  Even a blank input would just be empty not Null.

bol
Avatar of burnsj2

ASKER

devic: this doesn't seem to work
I put the snippets above in a valid html file and it works fine for me, like expected.  The problem is in some caused by some other part of the page, other script, or another element.

Let us know more about the page or try to use the general info I provided to id the specific issue.  Let me know if you want the html I used or have a question.

bol
Avatar of burnsj2

ASKER

Ok, I got the object model working.  (The real document is a lot more complex).  Thanks.
Any Idea why getElementById() doesn't work?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol