Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript help needed / writeto form

Status
Not open for further replies.

WizyWyg

Technical User
Jan 31, 2001
854
JP

Hi, am a beginner in JS (not my preferred language) but just want to accomplish something simple (Im guessing simple)

I have a form that has a text box that needs to be filled with a "time". The time is gotten by a list of text links that when clicked, will fill that text box with that time.

sample below:
Code:
<script language=&quot;javascript&quot;>
<!--
function writeTimeTag(code)
{
var cache = document.time.shout.value;
this.code = code;
document.time.shout.value = cache + code ;
document.time.shout.focus();
}
//-->
</script>

<form name=&quot;time&quot; method=&quot;post&quot;>
  <input type=&quot;text&quot; name=&quot;shout&quot; size=&quot;8&quot; maxlength=&quot;8&quot;>
  <a name=&quot;smilies&quot;></a> <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('09:00:00')&quot; >9:00</a> 
              <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('09:30:00')&quot; >9:30</a> 
              <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('10:00:00')&quot; >10:00</a> 
              <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('10:30:00')&quot; >10:30</a> 
              <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('11:00:00')&quot; >11:00</a> 
              <a href=&quot;#times&quot; onClick=&quot;writeTimeTag('11:30:00')&quot; >11:30</a> <br>
 
</form>


So, my question is:

When someone clicks on the link, how can I prevent them from adding more 'times' to the text box. As it is now, if they click on 9:00 it'll put in the box 09:00:00 (good) but if they accidentally also, hit 9:30, it will also be added to the text box (ie 09:00:0009:30:00 ). I put a maxlength of 8 characters, but it ignores that attribute. How can I get it to &quot;clear&quot; the field and then write the time if they chose wrong?

 
By not inserting the previous value in front of the new one:

Code:
function writeTimeTag(code)
{
//var cache = document.time.shout.value; //not needed
//this.code = code; // not needed
document.time.shout.value = code ;
document.time.shout.focus();
}

Should be all you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top