Hello and welcome to our community! Is this your first visit?
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
    Thanked 0 Times in 0 Posts

    Mirror textbox doesn't work on second try


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

    <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)">
    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 10:29 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Thanked 570 Times in 563 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
    <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">

  • 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