+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: textarea and link

  1. #1
    liguehs is offline x10Hosting Member liguehs is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    62

    textarea and link

    Hi guys,
    OK so here is my problem.
    What I want to do is that, everytime a user clicks on a link, the value of the link gets inserted in a textarea. When the user clicks on another link, that value is on another line.
    For exemple there is 3 links:
    Link 1 , Link 2 , Link 3
    And the users clicks on link 1 and link 3. So the texarea must show:
    Link 1,
    Link 3,
    And if a user wants to take out a value, they have to right click on it.
    For exemple, I would like to take out Link 3, so I right click link 3 in the textarea, and it doesnt show.
    So far, I am able to insert the values in the textarea, but not in different lines...
    It shows Link1,Link3...
    Here is the Javascript Code:
    Code:
     
    <script language="javascript" type="text/javascript">
    function addtext(text) {
        document.players.players1.value += text+",";
    }
    </script>
    And here is where the links are, which is pullen using DB:
    Code:
            
    echo"<table border='1' width='100%'>
        <tr>
            <td>
                <table width='100%'>
                    <tr>
                        <th colspan='6' bgcolor='#444444'><font color='#FFFFFF'>$user_team</font></th>
                    </tr>";
                    $result = mysql_query("SELECT * FROM `playersratings`  WHERE `Team` ='$ProId' OR `Team` = '$FarmId' ",$link);
                    while($row = mysql_fetch_array($result))
            {
            $name = "" .$row['Name']. "";
            echo "<tr><td><a href='#' onclick=\"addtext('$name')\"\>$name</a></td></tr>";
            }
    ?>
    And here is the textarea code:
    Code:
     
    <textarea name='players1' COLS="20" ROWS="10" type="text"></textarea>
    Thanks for your help,
    Ara
    Last edited by liguehs; 07-30-2009 at 10:45 AM.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: textarea and link

    Step 1. You have to capture the right click on the link by adding a "oncontextmenu" handler for the link.

    Code:
     
     
    echo "<tr><td><a href='#' onclick=\"addtext('$name')\"      
           oncontextmenu=\"removetext('$name')"\>$name</a></td></tr>";
    Step 2. You have to define removetext( txt ) so that it removes the text from the text area.
    a) pull text from text area
    b) find first occurance of url
    c) find substrings in front and behind, and join them
    d) put resulting text into text area

    Code:
     
    <script language="javascript" type="text/javascript">
    function removetext(txt) {
     
        orig_text = document.players.players1.value
        startAt = orig_text.search(txt );
        txt_len = txt.length ;
        new_text = orig_text.substring( 0, startAt - 1 ) + 
                           orig_text.substring( startAt + txt_len ) ;
     
       document.players.players1.value = new_text ;
    }
     
    </script)
    Might be one-off on and does not include error checking (ie if you right click on a url that is not in text area. Also, does not remove multiple occurances of a url.
    Last edited by descalzo; 07-30-2009 at 12:26 PM.

  3. #3
    liguehs is offline x10Hosting Member liguehs is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    62

    Re: textarea and link

    Thanks alot, but :
    Code:
    +",/n";
    How to remove it with it?
    Edit:
    And is it possible to restrain the user from adding only once the same value?
    Last edited by liguehs; 07-30-2009 at 12:59 PM. Reason: Automerged Doublepost

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: textarea and link

    Quote Originally Posted by liguehs View Post
    Code:
    +",/n";
    How to remove it with it?
    Sorry if this comes across a bit pedantic.

    I showed you how to remove the url. You want to adjust the script so that it will remove ",\n". Well, just add ",\n" to the text passed to the script.

    Code:
    <script language="javascript" type="text/javascript">
    function removetext(txt) {
        txt = txt + ",\n" ;
        orig_text = document.players.players1.value
        startAt = orig_text.search(txt );
        txt_len = txt.length ;
        new_text = orig_text.substring( 0, startAt - 1 ) + 
                           orig_text.substring( startAt + txt_len ) ;
       document.players.players1.value = new_text ;
    }
    </script>
    And is it possible to restrain the user from adding only once the same value?
    If you notice, I used the "search" function on the text from the text area to find the url you want to remove. It gives the position where the url starts (I stored that value in "startAt"). If it is not found, "search" returns -1.

    So, what you have to do is rewrite your addtext(text) function to search for text (the parameter) in document.players.players1.value + ",\n" If the search returns -1, you add the url. If the search returns anything else, you do not.
    Last edited by descalzo; 07-30-2009 at 02:06 PM.

  5. #5
    liguehs is offline x10Hosting Member liguehs is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    62

    Re: textarea and link

    Oh right sorry,

    I guess I was still sleepy a bit earlier.

    Thanks again.
    Ara
    Edit:
    I kinda of a problem with the code and I cant put my finger on it...

    Well, ok so I click on the link and it gets inserted in the text area, and I right click and it goes away.....

    But, when I add a 2 links and I right click on one, only some of the words dissapears...

    Also I was wondering, would it be possible to right click on the text area to make it disapear?
    Last edited by liguehs; 07-30-2009 at 03:29 PM. Reason: Automerged Doublepost

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: textarea and link

    Also I was wondering, would it be possible to right click on the text area to make it disapear?
    Assume you want the entire textarea widget to disappear (just hidden, not removed).

    Right clicks are caught by "oncontextmenu", as the above code.

    Code:
     
    <textarea name='players1'  oncontextmenu="hideMe()" COLS="20" ROWS="10" type="text"></textarea>

    To hide or show objects, you adjust the .style.display property to "none" (to hide) or "" (to show).

    Code:
        function hideMe(){
         document.getElementById("players1").style.display = "none"
        }
    To make it re-appear when he right clicks on a link and you add the url to the text area, add this line to the addtext function:

    Code:
    document.getElementById("players1").style.display = ""

  7. #7
    liguehs is offline x10Hosting Member liguehs is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    62

    Re: textarea and link

    No, not the whole textarea, just the value he right clicks on...
    Is it possible? I doubt it...

    and also:
    I kinda of a problem with the code and I cant put my finger on it...

    Well, ok so I click on the link and it gets inserted in the text area, and I right click and it goes away.....

    But, when I add a 2 links and I right click on one, only some of the words dissapears...

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: textarea and link

    RE: making line of text in textarea disappear when right clicked

    Cannot think of a way to do it.

    RE: leaving some text when two lines in textarea

    Maybe if you post your new code and/or link to the page that uses them

  9. #9
    liguehs is offline x10Hosting Member liguehs is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    62

    Re: textarea and link

    I cant give you the link as this part is a restriced area for the members, and I cant mess up with the system..

    But here is the code
    Code:
    <script language="javascript" type="text/javascript">
    function addtext(text) {
    orig_text = document.players.players1.value
    startAt = orig_text.search(text);
    if (startAt = "-1")
    {
      document.players.players1.value += text+",\n";
      }
    
    
    }
    
    function removetext(txt) {
        txt = txt + ",\n" ;
        orig_text = document.players.players1.value
        startAt = orig_text.search(txt );
        txt_len = txt.length ;
        new_text = orig_text.substring( 0, startAt - 1 ) +
                           orig_text.substring( startAt + txt_len ) ;
       document.players.players1.value = new_text ;
    }
    
    </script>
    And the link
    Code:
    echo "<tr><td><a href='#' onclick=\"addtext('$name')\" oncontextmenu=\"removetext('$name')\">$name</a></td></tr
    Thanks again for your help,
    Ara

  10. #10
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: textarea and link

    Code:
    function addtext(text) {
    orig_text = document.getElementById("players1").value
    startAt = orig_text.search(text);
    if (startAt ==  -1)
    {
      document.getElementById("players1").value += text+",\n";
      }
    }
    function removetext(txt) {
        txt = txt + "," ;
        orig_text = document.getElementById("players1").value 
        startAt = orig_text.search(txt );
        txt_len = txt.length ;
       new_text = orig_text ;
     
      if( startAt > -1 ){
       if( startAt > 0 ){
           new_text = orig_text.substring( 0, startAt - 1 ) +
                          orig_text.substring( startAt + txt_len + 1 ) ;
       } else {
     
          new_text = orig_text.substring( txt_len + 2 ) ;
       }
      }
     
        document.getElementById("players1").value  = new_text ;
    }
    Note that you test for equality with '==' in javascript, not '='
    And you test against the number -1, not the string "-1"

    Tested on a mock-up page and works. Had to split into two cases, when it is the first item (ie startAt is 0, so that the first substring and other cases.
    Last edited by descalzo; 07-30-2009 at 06:57 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 11-20-2008, 06:54 AM
  2. How do you make a textarea with a scrollbar?
    By Album in forum Tutorials
    Replies: 10
    Last Post: 10-23-2005, 04:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers