0

Yes I had an old post, nearly same question, yet when I took the time and wrote this myself instead of using @Tim Downs code I got closer than I even thought I would. Only problem now is that each time I click the button it adds another piece so best way to show you is by going to the bin yourself and trying it out

http://jsbin.com/ogiyed/1/edit

Code is here:

function getSelected() {
     if(window.getSelection) { return window.getSelection(); }
    else if(document.getSelection) { return document.getSelection(); }
                else {
                        var selection = document.selection && document.selection.createRange();
                        if(selection.text) { return selection.text; }
            return false;
        }
        return false;
    }

$('.selections').not('.username').mouseup(function(e) {
var uid = $('.selections').find('.username').text();
var selection = getSelected();
if(selection) {
$('button').click(function() {
var text = $('#textarea_content');
text.val(text.val()+'[quote="'+ uid +'"]'+ selection +'[/quote]');
});
}
});

It nearly works like a charm ;)

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • Can someone help with the newest issue at hand since @nnnnnn helped with the multi post, now since I added the code to my site it won't add the selection.. jsbin.com/ogiyed/8/edit – EasyBB Mar 07 '13 at 23:01

1 Answers1

1

You are assigning a click handler inside the mouseup handler, so on every mouseup it will add an additional click handler, all of which will then execute on every button click.

If you need to redefine an existing click handler you'd first remove the previous one with .off('click') and then assign a new one:

$('button').off('click').click(function() {
   var text = $('#textarea_content');
   text.val(text.val()+'[quote="'+ uid +'"]'+ selection +'[/quote]');
});

Updated demo: http://jsbin.com/ogiyed/4/

(As an aside: indent your code!)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Thanks for the side note, ok Ill take a peek at your code, I already looked at it and its working the way I want. Just having the issue of if you select Username and the content area it post that, as well if you just select the usersname. I only want this to work in the .selections area. – EasyBB Mar 07 '13 at 20:27
  • Hey NNNNNN can you help with this? Its not working now that I tried implementing it to my site http://jsbin.com/ogiyed/8/edit – EasyBB Mar 07 '13 at 21:53
  • Any suggestions on the above bin? I can't get the quote button to work now. – EasyBB Mar 08 '13 at 17:55
  • http://jsbin.com/ogiyed/14/edit There were several problems. I've fixed a couple: (a) You had an extra `}` at the end, which was obvious once I indented the code more consistently though both JSBin and the browser reported that problem. (b) `$(this).find('.username a').text();` doesn't find anything within the click handler because `this` is the clicked element and it doesn't contain the user name. Change it to `$(document).find('.username a').text();` and it works. – nnnnnn Mar 13 '13 at 10:09
  • (c) Clicking "quote" removes the selection before you can retrieve the text of what was selected, because the word "quote" is itself text that can be selected. Change that from plain text to a button and it works: http://jsbin.com/ogiyed/16/ – nnnnnn Mar 13 '13 at 10:09