Prevent input when text fills visible length of input
I have an html input that is absolutely positioned over a canvas element. The width is set to meet the right border of a canvas element in the background (highlighted).
The input is actually appended to the larger canvas element, based on where the user drops the green circle to the left of the input:
var input = jQuery('<div><input id="edit-annotation-input" ' +
'style="z-index: 100; position: absolute; top:' + (this.textPosition.y - 9) + 'px; left:'+ (this.textPosition.x + 8) +'px; width: ' + (610 - 7 - this.textPosition.x) + 'px; border: none; color: #0bac3a; outline: none; background-color: transparent !important; padding: 0px; font-family: arial; font-size: 12pt; " ' +
'spellcheck="false" type="text" name="mytext" value="'+string+'"/></div>');
jQuery("div #pad").append(input);
How can I prevent user input when the text fills the visible width of the input. The text should be stopped in the following example, when the user enters another key the input should be prevented:
"dsadss"
I want to avoid the text cursor just moving along as more input is entered i.e like this which is the default behavior:
"dsadsssss"
I have previously considered using properties such as
maxlength
and the oninput
event but could not come up with any combination that had this desired effect javascript jquery html css input
add a comment |
I have an html input that is absolutely positioned over a canvas element. The width is set to meet the right border of a canvas element in the background (highlighted).
The input is actually appended to the larger canvas element, based on where the user drops the green circle to the left of the input:
var input = jQuery('<div><input id="edit-annotation-input" ' +
'style="z-index: 100; position: absolute; top:' + (this.textPosition.y - 9) + 'px; left:'+ (this.textPosition.x + 8) +'px; width: ' + (610 - 7 - this.textPosition.x) + 'px; border: none; color: #0bac3a; outline: none; background-color: transparent !important; padding: 0px; font-family: arial; font-size: 12pt; " ' +
'spellcheck="false" type="text" name="mytext" value="'+string+'"/></div>');
jQuery("div #pad").append(input);
How can I prevent user input when the text fills the visible width of the input. The text should be stopped in the following example, when the user enters another key the input should be prevented:
"dsadss"
I want to avoid the text cursor just moving along as more input is entered i.e like this which is the default behavior:
"dsadsssss"
I have previously considered using properties such as
maxlength
and the oninput
event but could not come up with any combination that had this desired effect javascript jquery html css input
1
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15
add a comment |
I have an html input that is absolutely positioned over a canvas element. The width is set to meet the right border of a canvas element in the background (highlighted).
The input is actually appended to the larger canvas element, based on where the user drops the green circle to the left of the input:
var input = jQuery('<div><input id="edit-annotation-input" ' +
'style="z-index: 100; position: absolute; top:' + (this.textPosition.y - 9) + 'px; left:'+ (this.textPosition.x + 8) +'px; width: ' + (610 - 7 - this.textPosition.x) + 'px; border: none; color: #0bac3a; outline: none; background-color: transparent !important; padding: 0px; font-family: arial; font-size: 12pt; " ' +
'spellcheck="false" type="text" name="mytext" value="'+string+'"/></div>');
jQuery("div #pad").append(input);
How can I prevent user input when the text fills the visible width of the input. The text should be stopped in the following example, when the user enters another key the input should be prevented:
"dsadss"
I want to avoid the text cursor just moving along as more input is entered i.e like this which is the default behavior:
"dsadsssss"
I have previously considered using properties such as
maxlength
and the oninput
event but could not come up with any combination that had this desired effect javascript jquery html css input
I have an html input that is absolutely positioned over a canvas element. The width is set to meet the right border of a canvas element in the background (highlighted).
The input is actually appended to the larger canvas element, based on where the user drops the green circle to the left of the input:
var input = jQuery('<div><input id="edit-annotation-input" ' +
'style="z-index: 100; position: absolute; top:' + (this.textPosition.y - 9) + 'px; left:'+ (this.textPosition.x + 8) +'px; width: ' + (610 - 7 - this.textPosition.x) + 'px; border: none; color: #0bac3a; outline: none; background-color: transparent !important; padding: 0px; font-family: arial; font-size: 12pt; " ' +
'spellcheck="false" type="text" name="mytext" value="'+string+'"/></div>');
jQuery("div #pad").append(input);
How can I prevent user input when the text fills the visible width of the input. The text should be stopped in the following example, when the user enters another key the input should be prevented:
"dsadss"
I want to avoid the text cursor just moving along as more input is entered i.e like this which is the default behavior:
"dsadsssss"
I have previously considered using properties such as
maxlength
and the oninput
event but could not come up with any combination that had this desired effect javascript jquery html css input
javascript jquery html css input
edited Nov 26 '18 at 4:11
morganwebdev
asked Nov 26 '18 at 3:55
morganwebdevmorganwebdev
14510
14510
1
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15
add a comment |
1
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15
1
1
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15
add a comment |
1 Answer
1
active
oldest
votes
This is a example hope it will help you or you can refer to it to help your problem.
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474598%2fprevent-input-when-text-fills-visible-length-of-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a example hope it will help you or you can refer to it to help your problem.
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
add a comment |
This is a example hope it will help you or you can refer to it to help your problem.
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
add a comment |
This is a example hope it will help you or you can refer to it to help your problem.
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
This is a example hope it will help you or you can refer to it to help your problem.
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
$("input[name=testInput]").on('keydown keyup',function(event){
var $that = $(this);
var widthOfInput = getWidthOfInput(document.getElementById("testInput")) ;
var cssWidth = $that.width();
//console.log('widthOfInput'+ widthOfInput);
//console.log('widthInputCss'+ widthInputCss);
// accept key backspace
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ){
return true;
}
// check limit input
if(cssWidth < widthOfInput){
event.preventDefault();
}
});
function getWidthOfInput(inputEl) {
var tmp = document.createElement("span");
tmp.className = "input-element tmp-element";
tmp.innerHTML = inputEl.value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
document.body.appendChild(tmp);
var theWidth = tmp.getBoundingClientRect().width;
document.body.removeChild(tmp);
return theWidth;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="testInput" name="testInput" style="width:100px" />
answered Nov 26 '18 at 4:35
son phamson pham
27816
27816
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
add a comment |
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
A slightly modified form of this answer got the job done. The only thing I had to do was make the font size and family of the temporary element the same as the one I was trying to measure the text inside the input of to make the comparison work.
– morganwebdev
Nov 27 '18 at 15:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474598%2fprevent-input-when-text-fills-visible-length-of-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
is the width of the input dynamic? why didn't maxlength work for you? please include your markup
– billynoah
Nov 26 '18 at 3:58
@billynoah Yes it is dynamic. I have edited in (sorry for formatting I couldn't get it quite right yet)
– morganwebdev
Nov 26 '18 at 4:12
You should create snippet so we can see the problem in action.
– Just code
Nov 26 '18 at 4:15