Here i am using summernote editor for that i need to restrict space at frist position.
<link href="~/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.css" rel="stylesheet">
<div class="QB-PanelName"><lable>Question</lable></div>
<textarea name="QBQuestion" id="QBQuestion" rows="10" cols="80"></textarea>
</div>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="~/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.js"></script>
$('#QBQuestion').summernote({
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
['font', ['strikethrough', 'superscript', 'subscript']],
['fontsize', ['fontsize']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['picture'],
['table']
]
});
Here keypress is not working to restrict space in first position
$("#QBQuestion").keypress(function (evt) {
var kc = evt.data.keyCode;
var qbQuestion = $('#QBQuestion').summernote('code');
if (kc === 32 && qbQuestion.length === 0) {
event.preventDefault();
}
});
You need to make following changes in your script for keypress event.
"#QBQuestion"
to "div.note-editing-area div.note-editable"
.var kc = evt.data.keyCode;
to var kc = evt.keyCode;
.(kc === 32 && (qbQuestion.length == 0 || qbQuestion == '<p><br></p>'))
.So your whole code should look like below:
$('#QBQuestion').summernote({
toolbar: [
['style', ['bold', 'italic', 'underline', 'clear']],
['font', ['strikethrough', 'superscript', 'subscript']],
['fontsize', ['fontsize']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['picture'],
['table']
]
});
$("div.note-editing-area div.note-editable").keypress(function (evt) {
var kc = evt.keyCode;
var qbQuestion = $('#QBQuestion').summernote('code');
if (kc === 32 && (qbQuestion.length == 0 || qbQuestion == '<p><br></p>')) {
event.preventDefault();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.css" rel="stylesheet">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="QB-PanelName"><label>Question</label>
<textarea name="QBQuestion" id="QBQuestion" rows="10" cols="80"></textarea>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote-lite.js"></script>
Reasons for all the changes are:
Hope it helps and solved your issue.