Page 1 of 1

textarea and link

Posted: Thu Jul 30, 2009 10:39 am
by Aravinthan
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: Select all

 
<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: Select all

        
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: Select all

 
<textarea name='players1' COLS="20" ROWS="10" type="text"></textarea>
 
Thanks for your help,
Ara

Re: textarea and link

Posted: Thu Jul 30, 2009 11:01 am
by jackpf
You need to use \n.

Re: textarea and link

Posted: Thu Jul 30, 2009 11:47 am
by Aravinthan
Thanks,

And is there a way of taking away the value by right clicking on it?

And also, is there a way that the same value can be sent only once?

Thanks for your help,
Ara

Re: textarea and link

Posted: Thu Jul 30, 2009 11:51 am
by pickle
It may be possible, and it's not easy. You can determine the location of the cursor (the "I beam", not the mouse) via Javascript. How to do that exactly I'm not sure. Using that location & some fancy text work you can determine what link the user right clicked on.

To ensure links are unique, just search the value of the textarea for the value of the link you're adding. If it's found, don't add it.

It will almost certainly be easier to dynamically generate a new text field for each link the person clicks on, rather than messing with the value of a textarea.

Re: textarea and link

Posted: Thu Jul 30, 2009 12:06 pm
by Aravinthan
pickle,
I was thinking about it but, I am not that good....

For the textarea, I think you are right, getting a new input box would indeed be a better way, but how to limit it to only 1 tho?

Re: textarea and link

Posted: Thu Jul 30, 2009 12:11 pm
by jackpf
You could assign each input an id of the url, and if that element exists already, don't create a new one.

Re: textarea and link

Posted: Thu Jul 30, 2009 12:39 pm
by Aravinthan
Can you show me an exemple jackpf?

Re: textarea and link

Posted: Thu Jul 30, 2009 12:40 pm
by pickle
If you use jQuery, and give all your newly created textfields a particular class (let's say "targetlink"), it could be as simple as:

Code: Select all

$("a").click(function(){
  var href = $(this).val();
  if($("input.targetlink[value="+href+"]").length == 0)
    //add the link
}
That selector (the fancy part after the $ basically means: "select all <input> elements that have a class of targetlink and a value that is the same as the href of the link that was just clicked" If the number of elements selected is zero, then that link hasn't been added yet.

Re: textarea and link

Posted: Thu Jul 30, 2009 1:15 pm
by Aravinthan
Ok thanks alot xD