admin管理员组文章数量:1291314
Is it possible to have a contenteditable div in the browser that has maxlength - and it auto resizes until it hits the max length ?
Basically I want to have a editable elements that actually only consumes as much space as it is required to get displayed yet having the max length property .
EDITABLE_____________NORMAL WORD
I would like to make sure editalbe resize and only consumes as much as required . Reason is that I want to generate PDF out of this . So the extra spaces will be trimmed any way . So in browser too we should see the same. It is too plex to solve ?
EDITABLE NORMAL WORD
A related question I found .
Limiting Number of Characters in a ContentEditable div
Is it possible to have a contenteditable div in the browser that has maxlength - and it auto resizes until it hits the max length ?
Basically I want to have a editable elements that actually only consumes as much space as it is required to get displayed yet having the max length property .
EDITABLE_____________NORMAL WORD
I would like to make sure editalbe resize and only consumes as much as required . Reason is that I want to generate PDF out of this . So the extra spaces will be trimmed any way . So in browser too we should see the same. It is too plex to solve ?
EDITABLE NORMAL WORD
A related question I found .
Limiting Number of Characters in a ContentEditable div
Share Improve this question edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked Mar 2, 2014 at 21:05 NishantNishant 21.9k20 gold badges78 silver badges106 bronze badges 2-
1
Your
div
must have cssheight
ormax-height
set with anoverflow:scroll
or similar set because a div will automatically grow in height from the content without these set. – Rob Sedgwick Commented Mar 2, 2014 at 21:08 -
1
if you are referring to 'width' add css 'inline-block' to the div, to auto grow in width until it hits it's
max-width
css – Rob Sedgwick Commented Mar 2, 2014 at 21:11
4 Answers
Reset to default 2To make the contentEditable
fit neatly you simply need it to be an non-block-level element or set it to be such in CSS:
.textfield {display:inline;}
To limit the amount of characters that can be entered requires JavaScript...
First update your contenteditable element/s to include a max
attribute:
<div contenteditable="true" name="choice1" class="textfield" max="15">EDITABLE</div>
Then use JS to listen for key-presses on these elements and only allow them until the max-length has been reached:
var textfields = document.getElementsByClassName("textfield");
for(i=0; i<textfields.length; i++){
textfields[i].addEventListener("keypress", function(e) {
if(this.innerHTML.length >= this.getAttribute("max")){
e.preventDefault();
return false;
}
}, false);
}
Here's the DEMO
UPDATE: It's fairly trivial to extend this to include a min
length too. Here's an updated jsFiddle that does just that.
http://jsfiddle/KmFL6/ seems to work .
<strong>
<p>Hello
<span class="editable" contenteditable=true placeholder="Element"> </span>World
</p>
</strong>
Span being the key idea there . Span just spans across the area needed !
http://codepen.io/flesler/pen/AEIFc also is cool . CSS Property will give you a placeholder too .
[contenteditable=true]:empty:before {
content: attr(placeholder);
}
you can set the css to have max-width
and min-width
like so:
<style>
.some-selector{
max-width: 20em; /* or whatever you want */
min-width: 0px; /* this is only if it is empty */
width:auto;
word-wrap: break-word /* useful if you have really long words that would overflow otherwise*/
}
</style>
then in html as others are saying
<span contenteditable='true' class="some-selector"></span>
- Create contenteditable mon css sizing (flex, etc)
- Set contenteditable css:
word-break: break-all
ORbreak-word
本文标签: javascriptContenteditable div or span that resizes until it hits max lengthStack Overflow
版权声明:本文标题:javascript - Contenteditable div or span that resizes until it hits max length? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530322a2383717.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论