Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: Adjust date in textbox using keypress event
    primarykey
    data
    text
    <p>I have a standard <code>&lt;input&gt;</code> textbox where users enter a date. I'd like to add some functionality where users can enter <code>+</code> or <code>-</code> to add/subtract a day from the value. I added an <code>onkeypress</code> event that calls this function:</p> <pre><code>function adjustDate( element ) { var e = event || window.event; var code = e.charCode || e.keyCode; switch( code ) { case 43: // user pressed "+" (plus) if( isDate( element.value ) ) { var d = new Date(); d.getDate( element.value ); d.setDate( d.getDate() + 1 ); element.value = ( d.getMonth() + 1 ) + "/" + d.getDate() + "/" + d.getYear(); } else { var today = new Date(); element.value = ( today.getMonth() + 1 ) + "/" + today.getDate() + "/" + today.getYear(); } if( e.preventDefault ) e.preventDefault(); else e.returnValue = false; return false; element.select(); break; case 45: // user pressed "-" (minus) // ... } } </code></pre> <p>The <code>isDate</code> function I pulled off Google and works, but I can post it too if necessary.</p> <p>My problem is the works one time, then nada. For instance, if the textbox's value is <code>10/1/2009</code> then pressing <code>+</code> will make it <code>10/2/2009</code>, but no effect on subsequent plusses. Then, if I press <code>-</code> it goes to <code>9/30/2009</code> instead of back to <code>10/1/2009</code>, and ignores subsequent minus keypresses.</p> <p>I'm testing on IE8. This is an internal web app so other browsers would be nice but not necessarily required. I'm not currently using jQuery or any other framework, and for what this is would probably be overkill, so I'm looking for a pure Javascript solution. In fact, this feature is merely a "nice to have" and not something I need to spend much more time on.</p> <p>I know I'm either missing something or doing it entirely wrong. So which is it?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload