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
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form/Div problem

    I have a form field called "age" and it is a text field. I have javascript take the current year and subtract it by the age field and show the user's birth date. This is shown in a div.

    Here is my javascript code:
    Code:
    <script type="text/javascript">
    <!--
    function showDOB()
    {
    	var NewText = document.getElementById("DynamicText").value;
    	var DivElement = document.getElementById("TextDisplay");
    	DivElement.innerHTML = 2005 - NewText;
    }
    // -->
    </script>
    Then, I have this for my form and div fields:
    Code:
    		<tr>
    			<td>*Age:</td><td><input id="DynamicText" value="Numbers Only!" class="lighthover" onMouseOver="style.backgroundColor='#999900';" onMouseOut="style.backgroundColor='#009900';" onfocus="if(this.value=='Numbers Only!')this.value='';" onKeyUp="showDOB();" type="text" name="age" /></td>
    		</tr>
    		<tr>
    			<td>*Date Of Birth:</td><td width="10%"><font face="Times New Roman, Times, serif" color="#006600" size='3px'><div class="lighthover" onMouseOver="style.backgroundColor='#999900';" onMouseOut="style.backgroundColor='#009900';" id="TextDisplay">&nbsp;</div></font></td>
    		</tr>
    When I first load the page, it is a blank div, but when I type it changes. That is good. My problem is that when I clear the age field, the div goes to "2005". How I do get the div to go blank when the age field is changed to blank?

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    New Zealand
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Put an if in like this:
    if (NewText >0)
    DivElement.innerHTML = 2005 - NewText;
    else
    DivElement.innerHTML = ''; // thats two single quotes

    Paul
    ScrypTik Javascript editor with built in syntax error checking

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks I'll try it when I get home from school today!

    -Yaggles


  •  

    Posting Permissions

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