SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    instead of append, remove the previous

    I have these 2 pages. One page is a form with a text box field.

    The other one is an adress book pop-up page kinda thing.
    When user opens adress book pop-up, they click on a link and the textbox field in the form page is populated. If they click further on the array of the links on address page, the values are seperated by a comma.

    How do I limit (from the function AddTo()) the entry (or auto population) to just one, instead of comma seperated multiple?

    Thanks in advance.


    <!-- Form Page -->
    <script>
    function AddTo(addr) {
    var dms = document.compose_form.to_name;
    if(dms.value.length > 0) dms.value += ", ";
    document.compose_form.to_name.value += addr;
    }
    </script>

    <table border="0" cellspacing="0" cellpadding="2">
    <form ... name="compose_form">
    <tr><td class="fieldtitle">To/td>
    <td><input type="text" class="textbox" name="to_name" value=""></td><td><a href="#" onclick="window.open('abook.html',
    'win1','width=350,height=150,scrollbars=yes')">Contact book</a></td>
    </tr>
    <tr>
    <td>
    <input type="submit" class="databutton" value="Send"></td>
    <td colspan="2">&nbsp;</td>
    </tr></form></table>
    <!-- abook.html Adress book page -->
    <script type="text/javascript">
    <!--
    function addTo(to_list) {
    var isAdded = new Array();
    if(isAdded[to_list]) return;
    isAdded[to_list] = 1;
    var df = document.theForm;
    var wo = window.opener;
    wo.AddTo(to_list);
    }

    // -->
    </script>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr>
    <td nowrap><a name="to_link_2" href="javascript:if(window.navigator.appName!='Netscape') to_link_2.outerHTML='<b>[---]</b>';addTo('demo');" >[Add]</a> &nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width="88%">demo</td>
    </tr>
    <tr>
    <td nowrap><a name="to_link_1" href="javascript:if(window.navigator.appName!='Netscape') to_link_1.outerHTML='<b>[---]</b>';addTo('admin');" >[Add]</a> &nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width="88%">admin</td>
    </tr>
    </table>

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi johnoz,

    I played around with it a little

    Code:
    // Form Page
    
    var contacts;
    
    window.onload = function()
    {
      contacts = new ContactBook('compose_form', 'to_name');
    }
    
    function ContactBook(sFormName, sTextName) // object prototype
    {
      this.mul = false; // accept multiple contacts if true
      this.txt = document.forms[sFormName].elements[sTextName];
      this.add = function(sContact)
      {
        if (this.txt.value.length) {
          if (this.mul) {
            this.txt.value += ", " + sContact;
          }
        }
        else {
          this.txt.value = sContact;
        }
      }
      this.reset = function()
      {
        this.txt.value = "";
      }
      this.multiple = function(bAccept)
      {
        this.mul = bAccept;
      }
    }
    Code:
    <!-- abook.html Adress book page -->
    <script type="text/javascript">
    <!--
    function addTo(to_list) {
      window.opener.contacts.add(to_list);
    }
    
    // -->
    </script>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    ...
    </table>

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just noticed that I didn't actually answer the question in this thread's subject: "instead of append, remove the previous". Do you need to be able to "remove the previous" also?

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added a ContactBook method called remove, which will remove the specified contact. I haven't tested it, let me know if it doesn't work.

    Code:
    // Form Page
    
    var contacts;
    
    window.onload = function()
    {
      contacts = new ContactBook('compose_form', 'to_name');
    }
    
    function ContactBook(sFormName, sTextName) // object prototype
    {
      this.mul = false; // accept multiple contacts if true
      this.txt = document.forms[sFormName].elements[sTextName];
      this.add = function(sContact)
      {
        if (this.txt.value.length) {
          if (this.mul) {
            this.txt.value += ", " + sContact;
          }
        }
        else {
          this.txt.value = sContact;
        }
      }
      this.reset = function()
      {
        this.txt.value = "";
      }
      this.multiple = function(bAccept)
      {
        this.mul = bAccept;
      }
      this.remove = function(sContact)
      {
        var i, s = "", a = this.txt.value.split(", ");
        for (i = 0; i < a.length; ++i) {
          if (a[i] != sContact) {
            if (i) s += ", ";
            s += a[i];
          }
        }
        this.txt.value = s;
      }
    } // end ContactBook

  5. #5
    SitePoint Enthusiast johnoz's Avatar
    Join Date
    Mar 2002
    Location
    here
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the code.

    I appereantly solved my problem by changing the code to this:

    function AddTo(addr) {
    var dms = document.compose_form.to_name;
    //this line changed
    if(dms.value.length > 0) dms.value='';
    document.compose_form.to_name.value += addr;
    }

    I am not sure whether if it is a ghetto solution tho


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
  •