• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2683
  • Last Modified:

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.
0
burnsj2
Asked:
burnsj2
  • 5
  • 3
2 Solutions
 
b0lsc0ttIT ManagerCommented:
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
0
 
devicCommented:
if you are working with a form, use form object model (without getElementById)

e.g.
=============================================
<script language="JavaScript">
function uploadAll(obj)
{                  
      //check for non blank or default value
      var y = obj.form.newAlbumName;
      if(y.value == "" || y.value == "Enter Album Name")
      {
            alert("Album Name Required");
            return false;
      }
      else
      {
            alert("thanks! \n ------------- \n" + y.value);
      }
}
</script>

<form>
<input id="newAlbumName" type=text name='newAlbumName' value="Enter Album Name">
<input type="button" value="UploadAll" onClick="uploadAll(this);">
</form>
0
 
burnsj2Author Commented:
b0lsc0tt: the second alert is always appearing because the y.value is always NULL
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
b0lsc0ttIT ManagerCommented:
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
0
 
burnsj2Author Commented:
devic: this doesn't seem to work
0
 
b0lsc0ttIT ManagerCommented:
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
0
 
burnsj2Author Commented:
Ok, I got the object model working.  (The real document is a lot more complex).  Thanks.
Any Idea why getElementById() doesn't work?
0
 
b0lsc0ttIT ManagerCommented:
A duplicate ID is the most likely cause.  Really without more info it is just a guess (although being an expert means our guesses are pretty good). :)

I'm glad it's working.

bol
0
 
b0lsc0ttIT ManagerCommented:
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now