469,275 Members | 1,304 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

auto-populate textbox

Hi,

I have an html page that has both a textbox and a checkbox.
I need to autopopulate the textbox with a value of "0" when the checkbox is selected.

I am not sure which JS function to use.

Thaks in advance for your help. :-)

marcelo
Oct 25 '07 #1
6 2154
I shall be your savior :)
Well, if the effect your looking for is just when it's selected, and does not remove the 0 from the textbox when you check the checkbox, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.  var oCheckbox = document.getElementById('id of checkbox');
  3.  oCheckbox.onclick = function() {
  4.   document.getElementById('id of textbox').value = '0';
  5.  }
  6. }
  7.  
I could use DOM 2 event listeners, but this seems like a rather simple application so I'll leave it at that. Just reply if you wanted "0" to go away when you un-select the checkbox...
And WHY does the stupid thing say I'm a newbie? I don't know.
Oct 25 '07 #2
acoder
16,027 Expert Mod 8TB
And WHY does the stupid thing say I'm a newbie? I don't know.
Get to 32 posts and it'll go away :)
Oct 26 '07 #3
Get to 32 posts and it'll go away :)
Oh okay that's easy :) I thought it was measured by how long you've been registered.
Oct 26 '07 #4
I shall be your savior :)
Well, if the effect your looking for is just when it's selected, and does not remove the 0 from the textbox when you check the checkbox, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.  var oCheckbox = document.getElementById('id of checkbox');
  3.  oCheckbox.onclick = function() {
  4.   document.getElementById('id of textbox').value = '0';
  5.  }
  6. }
  7.  
I could use DOM 2 event listeners, but this seems like a rather simple application so I'll leave it at that. Just reply if you wanted "0" to go away when you un-select the checkbox...
And WHY does the stupid thing say I'm a newbie? I don't know.

I actually do need the "0" to go away when the checkbox is unchecked.
Thank you so much for your reply.
You are really are my savior :-)
Oct 26 '07 #5
Okay then, here's the code to do that.
Expand|Select|Wrap|Line Numbers
  1. window.onload = function() {
  2.     var oCheckbox = document.getElementById('id of checkbox');
  3.     var oTextbox = document.getElementById('id of textbox');
  4.     oCheckbox.onclick = function() {
  5.         if(this.checked) {
  6.             oTextbox.value = '0';
  7.         }
  8.         else {
  9.             oTextbox.value = '';
  10.         }
  11.     }
  12. }
I'm not sure if you were accepting input from users in the textbox, but I'll make a script so that if they un-check and then re-check the box their entered value is saved... So there ya go :)
Oct 28 '07 #6
Thank you very much!!!
Oct 30 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Manlio Perillo | last post: by
5 posts views Thread by Robert Downes | last post: by
20 posts views Thread by Vijay Kumar R. Zanvar | last post: by
5 posts views Thread by maya | last post: by
22 posts views Thread by nospam_news | last post: by
2 posts views Thread by Piotr K | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.