Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Addressing a text box from javascript

    OK, I'm obviously missing something basic about addressing elements on my webpage because I have this problem all the time.

    Here is the area of the web page in which the text box resides.
    Code:
                <div id="updateDateDiv">
    	<input type="text" value="" size="10" id="upDateDate" />
    	<input type="button" value="Update" onClick="SetUpdateDate()" />
    	Update Date
    	</div>
    and here is the javascript;
    Code:
    function SetUpdateDate(){
        var todaysDate = new Date();
    	var txtDate = monthName[todaysDate.getMonth()] + " " + todaysDate.getDay() + ", " + todaysDate.getYear();
    	document.upDateDate.value=txtDate;
    }
    This generates an error "upDateDate is null or not an object.

    Thanks for your help. I know I'll feel stupid when I see the answer.
    Scott Stewart
    Always happy to learn from pros.

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 1 Time in 1 Post
    I would suggest you use a form tag and change the "id" attribute to a "name" attribute , and include the form name in the javascript.


    So your htm would look like this.
    Code:
    <html>
    <head>
    <script type=" type="text/javascript"">
    function SetUpdateDate(){
        var todaysDate = new Date();
    	var txtDate = monthName[todaysDate.getMonth()] + " " + todaysDate.getDay() + ", " + todaysDate.getYear();
    	document.form1.upDateDate.value=txtDate;
    }
    </script>
    <head>
    <body>
    <form name="form1">
    
    
    <div id="updateDateDiv">
    	<input type="text" value="" size="10" name="upDateDate" />
    	<input type="button" value="Update" onClick="SetUpdateDate()" />
    	Update Date
    </div>
    
    </form>
    </body>
    </html>
    There maybe a better way to do this but this is how I do it.
    if ( atfirst != succeed)
    tryagain();

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Hello,

    Being it is just a snippit of code I would assume you have a form tag. In which case the proper syntax would be:

    document.forms[x].elements[x].value (x being the form or element number with in the document or form respectivly)

    or

    document.formName.FieldName.value

    or

    document.getElementById('fieldName').value

    Basscyst
    Last edited by Basscyst; 07-16-2004 at 09:37 PM.
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •