SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Location
    Rudgwick, UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing BB code in text-area?

    Hi,

    I'm really going round and round in circles with this

    Basically - someone would select some text in their TEXTAREA (#post_message), and then it would replace it with the respective BBtag wrapped around:

    Code:
    $('.markup_btn').click( function() { 
    	
    	var selected_txt_range = $('#post_message').getSelection();
    	var test = $('#post_message').val().substring(selected_txt_range.start,selected_txt_range.end);
    	var thevalue = $('#post_message').val();
    		thevaluenew = thevalue.replace(test,""+test+"");
    	alert("Replacing: " + test + ",  GOT: " + thevalue);
    	$('#post_message').val( thevaluenew );
    });
    The "test" var comes up fine with what I wanna replace... and using the getSelection() code I can get the start and end char (this is a plugin). The problem comes to when I wanna replace the actual selected part:

    Code:
    		thevaluenew = thevalue.replace(test,""+test+"");
    That works, BUT if you have for example:

    This i a test and a test again

    ...and you have the 2nd "test" highlighted, it would actually replace the first one (i.e it doesn't take into consideration the selected part).

    Is there a way to do a substr/regex replacement just using a sub-str? I tried http://reference.sitepoint.com/javas...ta/replaceData , but this doesn't seem to work? (I get an error about it not existing as a function)

    TIA - I've about had it with this function

    Andy

  2. #2
    SitePoint Addict
    Join Date
    Sep 2008
    Location
    Rudgwick, UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I knew as soon as I posted a question I would find an answer! Here's the solution in case anyone else is trying the same thing:

    Call with:

    Code:
    wrapTag("b")
    ..and it would wrap the selected text in xcxxx

    Code:
    				function wrapTag(tag) {
    				  var textarea = document.getElementById('post_message');
    				  wrapText(textarea, '[' + tag + ']', '[/' + tag + ']');
    				}
    				
    				function wrapText(element, pre_text, post_text) {
    					if (element.setSelectionRange) {
    						var start = element.selectionStart;
    						var end = element.selectionEnd;
    						element.value = element.value.substr(0, start) + pre_text + element.value.substr(start, end - start) + post_text + element.value.substr(end, element.value.length);
    						setSelectionRange(element, start + pre_text.length, end + pre_text.length);
    					}
    					else if (document.selection) {
    						element.focus();
    						var range = document.selection.createRange();
    						if (range.parentElement() != element)
    							return;
    						var len = range.text.length;
    						range.text = pre_text + range.text + post_text;
    						range.moveEnd('character', -post_text.length);
    						range.moveStart('character', -len);
    						range.select();
    					}
    					else {
    						element.value += pre_text + post_text;
    					}
    				}


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •