SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populate text boxes

    Hi all,
    I am dynamically creating text boxes using ASP. My typical text boxes are named dynamically as follow ,hrs0,hrs1,hrs2..hrs6...


    When users type something in the first box(hrs0), the javascript should carry the value of the first box to other ALL text boxes while typing...The value of text box could be a string or a number...

    Any help will be apprec...

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I'll give it a go when I get home in a few hours

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Okay, this should do.

    It's not a good idea to use slightly similar names to set up a dependency between them. Instead, make it explicit when you call them. This leads to more robust and portable code.

    I've extended the idea slightly to allow for updating a single box, or multiple ones. You'll see that it's easy to modify in that area.

    updatetextfield.js

    Code JavaScript:
    function updateTextField(source, destination) {
    	source = document.getElementById(source);
    	if (typeof destination == 'object') { // an array
    		for (var i=0, destLen = destination.length; i < destLen; i++) {
    			updateValue(source, document.getElementById(destination[i]));
    		}
    	} else if (typeof destination == 'string') {
    		updateValue(source, document.getElementById(destination));
    	}
    }
    function updateValue(source, destination) {
        /* if (source.value && destination.value) { */
    		destination.value = source.value;
        /*}*/
    }

    And some HTML to test it with

    Code HTML4Strict:
    <input type="text" id="hrs0" name="hrs0" value="" onkeyup="updateTextField('hrs0', ['hrs1', 'hrs2', 'hrs3', 'hrs4', 'hrs5' ,'hrs6'])" />
    <input type="text" id="hrs1" name="hrs1" />
    <input type="text" id="hrs2" name="hrs2" />
    <input type="text" id="hrs3" name="hrs3" />
    <input type="text" id="hrs4" name="hrs4" />
    <input type="text" id="hrs5" name="hrs5" />
    <input type="text" id="hrs6" name="hrs6" />
    <script type="text/javascript" src="updatetextfield.js"></script>
    Last edited by paul_wilkins; Dec 14, 2007 at 11:17.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will try it...Thank you though

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried it doesnt give any error but it doesnt place the value of first text box to others....

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    My apologies, I put in a test without testing it out.

    Please update the updateValue() function as follows

    Code JavaScript:
    function updateValue(source, destination) {
        destination.value = source.value;
    }

  7. #7
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?:

    Code JavaScript:
    function updateValues(value) {
    	for (var i = 0; true; i++) {
    		var el = document.getElementById('hrs' + i);
    		if (!el)
    			break;
    		else
    			el.value = value;
    	}
    }

    Add this to your first text box: onKeyUp="updateValues(this.value);"

    I wrote this in like 2 min but I tested it.

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    ny
    Posts
    560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much dudes D


Bookmarks

Posting Permissions

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