currently new js, php. tough transition! problem, live replacing characters in string.
basically have textarea, , live update of contents div below. on top of each live update replace '[' , ']' '<' , '>', , gets little bit more complex. display live html (sense html tags bbcode).
this have:
html -
<textarea id="my_textarea" name="my_textarea"></textarea> <input type="button" value="b" name="texteditorstylebuttonbold"/> <input type="button" value="i" name="texteditorstylebuttonitalicize"/> <input type="button" value="u" name="texteditorstylebuttonunderline"/> <div id="display"></div>
js -
function getinputselection(elem){ if(typeof elem != "undefined"){ start = elem[0].selectionstart; end = elem[0].selectionend; 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) }); $('my_textarea').onkeyup(function() { $('display').val() = $('#my_textarea').val().replace(new regexp("[", 'g'), "<"); });
before continue, other code, , inputs, used make style buttons code, , suppose bbcode. know not bbcode is, making own type of thing! :) problem though code not live update code... please help!
p.s. little example:
user enters in text area: [b] bold text [/b]
then live, replace [b] bold html tag , show user down below: this bold text
please ask if not understand problem is. thank you! :)
this means want append html content div. if want div recognize html tags, need use .html()
property:
$('#display').html($('#my_textarea').val());
also, need update part follows (onkeyup
should keyup
, please not forget include #
if using id selecter):
$('#my_textarea').keyup(function() { $('#display').html($('#my_textarea').val()); });
here working demo: https://jsfiddle.net/0uoshu0s/6/