javascript - selectionStart Grabbing Earlier Parts of String JS -
it's issue, not huge issue. mean ask? making bbcode editor, , having minor issues on highlight text part right now.
basically when user highlights text, , clicks instance bold button, insert [b] , [/b] around text. working fine, there issue!
lets textarea:
hello name michael. name? pay attention words 'name'! if highlight first 'name' word , bold it, works correctly. yet if highlight second 'name' , try bold it, instead bold first 'name' instead.
so if highlight word exists before it, , try style it, style same word beforehand. here full code:
function getinputselection(elem){ if(typeof elem != "undefined"){ start = elem[0].selectionstart; end = elem[0].selectionend; if (start != end) { return elem.val().substring(start, end); } } else{ return ''; } } $('input:button[name^="texteditorstylebutton"]').click(function() { var mytextareavalue = $('#my_textarea').val(); var selectedtext = getinputselection($('#my_textarea')); var updatedtext = '['+$(this).val()+']' + selectedtext + '[/'+$(this).val()+']'; mytextareavalue = mytextareavalue.replace(selectedtext, updatedtext); $('#my_textarea').val(mytextareavalue) }); $('input:button[name^="texteditorstylebutton"], #my_textarea').on('click keyup',function(){ var newer = $('#my_textarea').val().replace(/(\[((\/?)(b|u|i|s|sub|sup))\])/gi, '<$2>'); $('#display').html(newer); }); any ideas why doing this? think because of selectionstart. edit thank you, believed becuase of replace. ideas?
Comments
Post a Comment