wolfgang1983 wolfgang1983 - 9 months ago 25
Javascript Question

Preview tab not showing when press editor buttons

When I type in my textarea once has dected more than 4 characters it enables the preview tab.

If I click on my editor buttons example the button-bold it shows text like

**strong text**
but even though there is more than 4 characters preview tab does not enabled.

Currently It only enabled when keyup


Question: How can I make sure if I click on a editor button it will
dected how many characters and if greater than 4 will enable tab.


CodePen Example Demo

$('#tab-preview').hide();

$("#textarea_message").on('keyup', function(e){
if ($(this).val().length >= "4") {
$('#tab-preview').show();
}

if ($(this).val().length < "4") {
$('#tab-preview').hide();
}
});


HTML

<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<form id="form-ask">

<div class="form-group">
<button type="button" id="button-bold" class="btn btn-default" title="Bold"><i class="fa fa-bold" aria-hidden="true"></i></button>
<button type="button" id="button-italic" class="btn btn-default" title="Italic"><i class="fa fa-italic" aria-hidden="true"></i></button>
<button type="button" id="button-quote" class="btn btn-default" title="Quote"><i class="fa fa-quote-left" aria-hidden="true"></i></button>
<button type="button" id="button-code" class="btn btn-default" title="Code"><i class="fa fa-quote-left" aria-hidden="true"></i></button>
<button type="button" id="button-insert-image" data-toggle="modal" data-target="#insert-image" class="btn btn-default" title="Insert Image URL"><i class="fa fa-code" aria-hidden="true"></i>
</button>
<button type="button" id="button-upload" class="btn btn-default" title="Add Attachment: This can be image or file "><i class="fa fa-file" aria-hidden="true"></i>
Attach File</button>
</div>

<div class="panel ask panel-default">
<div class="panel-heading">
<ul class="nav nav-tabs">
<li class="active"><a data-target="#ask" id="tab-ask" data-toggle="tab"><i class="fa fa-code" aria-hidden="true"></i> Ask Question</a></li>
<li><a data-target="#preview" id="tab-preview" data-toggle="tab"><i class="fa fa-eye" aria-hidden="true"></i> Preview</a></li>
</ul>
</div>
<div class="panel-body">
<div class="tab-content">
<div class="tab-pane fade in active" id="ask">
<div class="form-group">
<textarea id="textarea_message" class="form-control"></textarea>
</div>
</div>
<div class="tab-pane fade" id="preview">
<div class="preview-message"></div>
</div>
</div>
</div>
</div>
<p class="text-muted">Preview Button will be enabled once your question has reached
4 character or more.</p>


</form>
</div>
</div>
</div>

Answer Source

One solution would be to add a 'trigger' for the keyup event on your buttons

$('#button-bold').on('click', function(e) {
    markdown_syntax(this.id, "**", "**", 'strong text');
    $("#textarea_message").trigger('keyup');
});