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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Mirror textbox doesn't work on second try

    Hi,

    I'm trying to mirror two textboxes with the following:

    Code:
    <form>
    <input type="text" id="0" name="tb1[]" onkeyup="document.getElementsByName('tb2[]')[this.id].setAttribute('value',this.value)">
    
    <input type="text" id="0" name="tb2[]" onkeyup="document.getElementsByName('tb1[]')[this.id].setAttribute('value',this.value)">
    </form>
    If I start by picking the first box and typing it appears automatically in the second textbox. It works the same if if i start by picking the second box. However after entering text into one of the textboxes and having it mirrored, if I select the other textbox and make a change it isn't mirrored.

    Any suggestions? Thanks!
    Last edited by johnfm; 07-01-2011 at 09:29 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You stumbled upon the difference between attributes and properties which is not always obvious in browsers.

    If you use setAttribute('value', '...') you set the attribute "value" for an element. But if you use .value on an attribute, you get or set the property "value" of the element.

    Let's take a look at the difference
    1. Assuming that the element has a value of "abc" the browser will render the value into the element and internally copy "abc" to the property "value"
    2. Now if you change the attribute "value" afterwards it will not change the property and will therefore not be visible in the element
    3. But if you set the property with .value, it will be visible because it's the property

    So try this
    Code:
    <form>
    <input type="text" id="0" name="tb1[]" onkeyup="document.getElementsByName('tb2[]')[this.id].value = this.value">
    
    <input type="text" id="0" name="tb2[]" onkeyup="document.getElementsByName('tb1[]')[this.id].value = this.value">
    </form>

  • Users who have thanked devnull69 for this post:

    johnfm (07-01-2011)


  •  

    Posting Permissions

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