1

Hey guys I'm trying to make it so when you click on the existing a href= it will paste the inner text to the textarea. Though of course if click that the a href it'll lead to link so i made it so it'll take out the href. So far I have this code

$(function(){
$(".username a").removeAttr("href").css("cursor","pointer");
var $paste = $('div.post h4 .username a span');
  $paste.click('')
});

Ok I guess thats a good start lol. Problem to me is, I don't know how to add onClick="" or whatever to make the function work on click ya know? or would .click work in general. of course i would need a function in the .click area. I don't know what to place in there to create what text is inside the a tag... Can someone help me, if you use .append or or anything can you also give an explanation, I'm trying to learn as I go.

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • 1
    Instead of removing the href, just `e.preventDefault();` in the click function. This tells jQuery to stop the event propogation and it will never reach the page switching code. http://api.jquery.com/event.preventDefault/ – TheZ Oct 19 '12 at 23:48
  • ok...? haha Ya lost me TheZ.So .click('e.preventDefault()')? how would I make so it would paste the text inside to the textarea? like {username} I would want that text to go into the textarea – EasyBB Oct 19 '12 at 23:51

2 Answers2

2
$(function(){
    $('.username a').click(function(){ return false; });
    $('.username a').one('click', function(e){
             //e is short for event
        e.preventDefault(); //will no longer follow through
        //You want to append the text of the anchor link into the textarea.

        var innerTxt = $(this).text();
        //need to trim whitespace from the string
        innerTxt = $.trim(innerTxt);

        var $obj= $('textarea'); //replace this with textarea selector
        $obj.val(
           $obj.val()+'\n@'+innerTxt
        );               
    });
});​
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • I like the above coding. Seems nice, jsFiddle nicely done, and you explained to me what each does (some I'm still curious lol.) but when in the jsFiddle, click once works, click twice adds TWO more not just one... – EasyBB Oct 20 '12 at 00:06
  • @user1760670 that's fine, that's an easy fix. you just want it to happen once? – Ohgodwhy Oct 20 '12 at 00:07
  • Also if I get this to work on my site, is it possible to do this '@'+innerText+':' ??? so when clicked it will be as so @Inner Text: – EasyBB Oct 20 '12 at 00:07
  • more like that http://jsfiddle.net/LWGhJ/1/ how do I get it to have no spaces? when click it shows @ Inner Text : I want to get rid of the spaces here @Inner Text: and will this work if there are multiple links of the section I want .username a? And it doesnt need to be once, it just when you clicked once it pasted once, then when you click again it had three altogether Inner Text Inner Text Inner Text... – EasyBB Oct 20 '12 at 00:13
  • Hahaha I see why it had the space, it was because the generated html had the space in it. Nevermind. I got it working fine. Just need to know if it'll work with multiple .username a and such? – EasyBB Oct 20 '12 at 00:15
  • Each generated element will only be allowed one click. The downside is that when you're done with it the click function will follow through again. Also, you should combat the whitespace issue by calling `innerTxt = $.trim(innerTxt);` This will ensure any leading/trailing whitespace is trimmed. Let me know if you want a solution to the `.one()` follow-through issue. – Ohgodwhy Oct 20 '12 at 00:20
  • I would :) i see how you did the .one() in your jsfiddle I rewrote to be back to .click because I saw that once .one() ran it went back to a href tag. Though once go through it created the same issue of creating a duplicate. Please see http://jsfiddle.net/LWGhJ/2/ and click every tag once – EasyBB Oct 20 '12 at 00:24
  • @user1760670 Just use the new jsFiddle. I've already handled the `.one()` issue. – Ohgodwhy Oct 20 '12 at 00:26
  • Only have 13 points so cant go into chat. Same issue persist, when clicking more than one it duplicates a main one. How to ensure that this does not happen? – EasyBB Oct 20 '12 at 00:29
  • @user1760670 - [Just like this](http://jsfiddle.net/P2Nuv/). We'll just remove the substring and selection methods and keep it simple. – Ohgodwhy Oct 20 '12 at 00:36
  • Love that one much better :) Though when going to my website it is not working for some reason...

    Mr.EasyBB

    – EasyBB Oct 20 '12 at 00:42
  • Love that one much better :) Though when going to my website it is not working for some reason...

    Mr.EasyBB

    Is html where a is located textarea id and class name are #text_editor_textarea .quick_reply_textarea
    – EasyBB Oct 20 '12 at 00:53
  • my bad on the double post. it wouldn't let me edit haha. It keeps being able to click to the actual href :/ – EasyBB Oct 20 '12 at 00:56
  • @user1760670 Are you sure that the jQuery library is loading properly? What browser are you using, we should check the console for errors. – Ohgodwhy Oct 20 '12 at 01:20
  • We should :) though Im not sure if this is helpful but the a href is generated through php {postrow.display.POSTERNAME} though i dont think it should matter since we are editing the generated html. Two syntaxErrors. P.S this code is to help another code for tagging. Uncaught SyntaxError: Unexpected token ILLEGAL – EasyBB Oct 20 '12 at 01:28
  • @user1760670 The error you're experiencing is not in my code. – Ohgodwhy Oct 20 '12 at 01:40
  • Ok I got rid of one error, your code is still experiencing some sort of syntax error. Im trying to figure it out now, maybe I messed something up – EasyBB Oct 20 '12 at 01:46
  • Hey Ohgodwhy could you come to my website, there is a chatroom, and it would be easier for you to see what is going on. the consoleError was from me adding this $('h4.username a') for some reason it doesnt like that haha. – EasyBB Oct 20 '12 at 01:58
  • www.easybbtutorials.forumotion.com you'll have to sign up if thats not a problem to see the chatroom – EasyBB Oct 20 '12 at 02:01
  • I see you created an account. :) Chat is at bottom of page ill be waiting – EasyBB Oct 20 '12 at 02:15
0

Try this

$(".username a").click(function(e){
    //DO Wathever you want here 
    e.preventDefault(); 
})
Etienne Desgagné
  • 3,102
  • 1
  • 29
  • 36