admin管理员组文章数量:1313176
I spent about 2 hours in searching for javascript which will give me easy options of adding new "onclick, insert text" buttons for textarea, but unfortunatelly I couldn't find what I am looking for.
I have textarea:
<textarea name="user-submitted-content" rows="5" data-required="true" placeholder="<?php _e('Post Content', 'usp'); ?>" class="usp-textarea"></textarea>
I want javascript something like this:
var newText = "[" + shortcode + "]" + selectedText + "[/" + shortcode + "]";
And than I can add buttons like:
<input type="button" value="youtube" onclick="formatText ('youtube');" />
<input type="button" value="text" onclick="formatText ('text');" />
When I click button "youtube" it should add/insert: [youtube][/youtube]
in textarea.
Above codes I took from one example of javascript which sorrounds selected text in textarea with tags:
<script type="text/javascript">
<!--
function formatText (tag) {
var selectedText = document.selection.createRange().text;
if (selectedText != "") {
var newText = "[" + tag + "]" + selectedText + "[/" + tag + "]";
document.selection.createRange().text = newText;
}
}
//-->
</script>
<form name="my_form">
<textarea name="my_textarea"></textarea><br />
<input type="button" value="bold" onclick="formatText ('b');" />
<input type="button" value="italic" onclick="formatText ('i');" />
<input type="button" value="underline" onclick="formatText ('u');" />
</form>
With a couple of changes, I think it can be used also for this what I want. I really need this.
I spent about 2 hours in searching for javascript which will give me easy options of adding new "onclick, insert text" buttons for textarea, but unfortunatelly I couldn't find what I am looking for.
I have textarea:
<textarea name="user-submitted-content" rows="5" data-required="true" placeholder="<?php _e('Post Content', 'usp'); ?>" class="usp-textarea"></textarea>
I want javascript something like this:
var newText = "[" + shortcode + "]" + selectedText + "[/" + shortcode + "]";
And than I can add buttons like:
<input type="button" value="youtube" onclick="formatText ('youtube');" />
<input type="button" value="text" onclick="formatText ('text');" />
When I click button "youtube" it should add/insert: [youtube][/youtube]
in textarea.
Above codes I took from one example of javascript which sorrounds selected text in textarea with tags:
<script type="text/javascript">
<!--
function formatText (tag) {
var selectedText = document.selection.createRange().text;
if (selectedText != "") {
var newText = "[" + tag + "]" + selectedText + "[/" + tag + "]";
document.selection.createRange().text = newText;
}
}
//-->
</script>
<form name="my_form">
<textarea name="my_textarea"></textarea><br />
<input type="button" value="bold" onclick="formatText ('b');" />
<input type="button" value="italic" onclick="formatText ('i');" />
<input type="button" value="underline" onclick="formatText ('u');" />
</form>
With a couple of changes, I think it can be used also for this what I want. I really need this.
Share Improve this question edited Oct 12, 2013 at 5:39 Mezelderz asked Oct 10, 2013 at 6:31 MezelderzMezelderz 1312 silver badges11 bronze badges2 Answers
Reset to default 3If am not wrong , you where looking for this
Sample DEMO 1
Sample DEMO 2
JS: Sample 1-
$("#btnYoutube").on('click', function () {
var setText = "[youtube][/youtube]";
$("#txtarea").val(setText);
});
$("#btnTxt").on('click', function () {
var setText = "[text][/text]";
$("#txtarea").val(setText);
});
Sample 2-
$("input:button").on('click',function(){
var getTxt=$(this).val();
var setText='['+getTxt+'] ['+getTxt+']';
$("#txtarea").val(setText)
});
Html:
<input id="btnYoutube" type="button" value="youtube" />
<input id="btnTxt" type="button" value="text" />
<br/>
<textarea id="txtarea" name="user-submitted-content" rows="5" data-required="true" class="usp-textarea"></textarea>
Append text in textarea:
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('mytextarea');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value += '[' + tag + ']' + '[/' + tag + ']';
}
</script>
<form name="my_form">
<textarea id="mytextarea"></textarea><br />
<input type="button" value="youtube" onclick="formatText ('youtube');" />
<input type="button" value="dailymotion" onclick="formatText ('dailymotion');" />
<input type="button" value="vimeo" onclick="formatText ('vimeo');" />
</form>
Replace text in textarea:
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('mytextarea');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value = '[' + tag + ']' + '[/' + tag + ']';
}
</script>
<form name="my_form">
<textarea id="mytextarea"></textarea><br />
<input type="button" value="youtube" onclick="formatText ('youtube');" />
<input type="button" value="dailymotion" onclick="formatText ('dailymotion');" />
<input type="button" value="vimeo" onclick="formatText ('vimeo');" />
</form>
Examples
Append: http://jsfiddle/aSgFa/
Replace: http://jsfiddle/bAQEs/
Also:
<script type="text/javascript">
function formatText(tag) {
var Field = document.getElementById('user-submitted-content');
var val = Field.value;
var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
var before_txt = val.substring(0, Field.selectionStart);
var after_txt = val.substring(Field.selectionEnd, val.length);
Field.value += '' + tag + '';
}
</script>
<div class="buttons">
<input type="button" value="youtube" onclick="formatText ('[video]YouTubeURL[/video]\n[description]Write your description[/description]');" />
</div>
本文标签: javascriptOnclickadd shortcodebbcode to textareaStack Overflow
版权声明:本文标题:javascript - Onclick, add shortcodebbcode to textarea? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741926531a2405321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论