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

    Sync with Javascript question

    Hi,

    I have a form with a input field(name) and a textarea(keywords).
    I am using the below code to sync the value of name with keywords.
    If additional keywords are added in the textarea and then the name field is altered all additional keywords are lost as they are overwritten by the name field sync.
    I would like to place a comma after the synced value and have whatever follows the comma to not be touched.
    Like this:

    Name: keyword1
    Keywords: keyword1, keyword2, keyword3, etc.

    Could anyone point me in the right direction?
    Any help with this would be most appreciated.

    Code:
    var obj=document.getElementById('name');
    var att = {"onkeyup":"sync()"};
    for(var n in att) {
        obj.setAttribute(n, att[n]);
        }
    function sync()
    {
      var v1 = document.getElementById('name');
      var v2 = document.getElementById('keywords');
      v2.value = v1.value;
    }

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    var obj=document.getElementById('name');
    obj.onkeyup = sync;
    
    function sync()
    {
      var v1 = document.getElementById('name');
      var v2 = document.getElementById('keywords');
      v2.value = (v2.value) ? ', ' + v1.value : v1.value;
    }
    The way you attach the onkeyup handler is not correct. Event handlers are not strings (as you were doing) but function pointers.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    'keywords' would seem to be a reserved word in IE, as it does not work. It does work if you change it to something else like 'keywds'.

    I am not sure that I understand what is wanted. But erasing the textbox erases the entire textarea.

  • #4
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for those tips. Not exactly what I am trying to achieve. Just to shed some more light on what I am after.

    The name field will be a user inputted value. I would like the name field value to be put at the very start of the keywords text area, trailed by a comma and space.

    For example:
    Someone enters "My Name is Bob!" into the name field.
    "My name is Bob!" is then placed at the very start of the Keywords textarea with a trailing comma and space, like this. "My name is Bob!, ".
    Then someone adds to the keywords textarea "flower shop" so it looks like "My name is Bob!, flower shop".

    The above example is not my issue. What I am after is when someone then alters the name field from "My Name is Bob!" to "Bob's Flower Shop" I would like that to change in the first comma seperated value of the Keywords textarea.

    Any ideas?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    What do you mean by "someone"? Are you envisaging a number of different users? If so you cannot use Javascript which is purely a client-side language.

  • #6
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Someone, anyone. Whom ever is using it. Lets call him Bob. I understand it is client side.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Well, what is the point of someone entering "My Name is Bob!" and "Flower Shop" into a textbox that only that user can see?

  • #8
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Those values are going to be saved to a database and then output somewhere else. I just was using those as an example. It could be anything. Why does there have to be a point? What is with this line of questioning? It is irrelevant.

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I worked it out.

    Code:
    function sync()
    {
      var v1 = document.getElementById('name');
      var v2 = document.getElementById('keywords');
      var valofv2 = v2.value;
      var splitv2 = valofv2.split(",");
      splitv2[0] = v1.value;
      v2.value = splitv2;
    }
    The only problem left is if I add a comma in the name field it screws the whole thing up.
    Last edited by JMJdev; 11-21-2010 at 01:38 AM. Reason: issue

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,050
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Remove the comma

    Code:
    splitv2[0] = v1.value.replace(/\,/g, "");

  • Users who have thanked glenngv for this post:

    JMJdev (11-23-2010)

  • #11
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv View Post
    Remove the comma

    Code:
    splitv2[0] = v1.value.replace(/\,/g, "");


    Works like a charm. Thanks Glenn!

  • #12
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Adding onto the current function I would like the name field to sync with the the correct split of the keywords textarea even if it is not in position 1.

    This is what I have tried but I am new to this and it is not working.

    Code:
    function sync()
    {
      var v1 = document.getElementById('name');
      var v2 = document.getElementById('keywords');
      var valv1 = v1.value;
      var valv2 = v2.value;
      var splitv2 = valv2.split(",");
      
      for(i = 0; i < splitv2.length; i++){
      if(splitv2[i] == valv1) {
    	  splitv2[i] = v1.value.replace(/\,/g, "");
      }
      
      }
      v2.value = splitv2;
    }
    Thanks


  •  

    Tags for this Thread

    Posting Permissions

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