#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    21
    Rep Power
    0

    Update one text field when other is changed


    How do I set one text value as another using JS? I had this working before and completely forgotten how/why it isn't working now!!

    I'm sure it'll be easy for someone to look at the code below and see why it isn't updating the url field when the title field is changed.

    Code:
     
    JAVASCRIPT
    
      function ChangeURL() {
    var tb1 = document.getElementById('questiontitle').str.replace(/[^a-zA-Z\s]/g,"").replace(/[\s]/g,"-").str.toLowerCase();
    
    document.getElementById('questionURL').innerHTML = tbl.value;
      }
    Code:
    <textarea id="questiontitle" name="questiontitle" cols="100" rows="3" onchange="ChangeURL()"></textarea>
    <input name="questionURL" type="text" value="add-a-filename-here.php" size="92"/>
    But it doesn't update onchange - I got this working before and now I can't seem to get it.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,041
    Rep Power
    119
    Should work now; plus I added in a period in your allowed regex characters, because if you don't; it will be stripped away from end user's renaming of file.
    Code:
    <script>
    function ChangeURL()
    {
    var tb1 = document.getElementById('questiontitle').value.replace(/[^a-zA-Z\s\.]/g,"").replace(/[\s]/g,"-").toLowerCase()
    document.getElementById('questionURL').value = tb1;
    }
    </script>
    
    <textarea id="questiontitle" name="questiontitle" cols="100" rows="3" onchange="ChangeURL()"></textarea>
    <input id="questionURL" type="text" value="add-a-filename-here.php" size="92"/>
    By the way, the problem was: you were calling the id of the input element; but you did not have an id attribute for the input element, instead you had a name attribute. And... you had "tbl" (miss-spelled); instead of the variable "tb1"; when you were trying to change the value of the input element. Plus..., you was using innerHTML and not value; like you should have been. Oh, and... ".value" did not need to be applied to the end of "tb1"; when you were changing the value of the input element. You should have used that (".value"), before your regular expression; to get the content of the textarea. You was using the variable "str"; which..., was an undefined variable, because it was no where in the code you posted (in your original post).
    Last edited by web_loone08; July 11th, 2013 at 12:30 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    21
    Rep Power
    0
    Originally Posted by web_loone08
    Should work now; plus I added in a period in your allowed regex characters, because if you don't; it will be stripped away from end user's renaming of file.
    Code:
    <script>
    function ChangeURL()
    {
    var tb1 = document.getElementById('questiontitle').value.replace(/[^a-zA-Z\s\.]/g,"").replace(/[\s]/g,"-").toLowerCase()
    document.getElementById('questionURL').value = tb1;
    }
    </script>
    
    <textarea id="questiontitle" name="questiontitle" cols="100" rows="3" onchange="ChangeURL()"></textarea>
    <input id="questionURL" type="text" value="add-a-filename-here.php" size="92"/>
    By the way, the problem was: you were calling the id of the input element; but you did not have an id attribute for the input element, instead you had a name attribute. And... you had "tbl" (miss-spelled); instead of the variable "tb1"; when you were trying to change the value of the input element. Plus..., you was using innerHTML and not value; like you should have been. Oh, and... ".value" did not need to be applied to the end of "tb1"; when you were changing the value of the input element. You should have used that (".value"), before your regular expression; to get the content of the textarea. You was using the variable "str"; which..., was an undefined variable, because it was no where in the code you posted (in your original post).
    Thank you! Sometimes you just need someone to look at it - I've been staring at the screen for too long..

IMN logo majestic logo threadwatch logo seochat tools logo