textarea and link

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

textarea and link

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: textarea and link

Post by jackpf »

You need to use \n.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: textarea and link

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: textarea and link

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: textarea and link

Post 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?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: textarea and link

Post by jackpf »

You could assign each input an id of the url, and if that element exists already, don't create a new one.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: textarea and link

Post by Aravinthan »

Can you show me an exemple jackpf?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: textarea and link

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Aravinthan
Forum Commoner
Posts: 84
Joined: Mon Jan 28, 2008 6:34 pm

Re: textarea and link

Post by Aravinthan »

Ok thanks alot xD
Post Reply