admin管理员组文章数量:1431955
I'm looking for a way to run some JS when a textarea gets resized.
After an hour of searching and fiddling I have something that kinda works, but it is not good enough.
titleTextArea.mouseup(function() {
popup.update(); titleTextArea.focus();
});
The problem with the above code is that it also runs when clicking in the textarea. The code I am running causes the textarea to get re-rendered, which should not happen while someone is working in it, as it messes with the focus.
I've tried using jQuery resizable as per this SO post. For some reason the resize event does not fire. And really I'd prefer not needing to pull in jQuery UI for this.
Is there a way to run code on textbox resize (only on pletion of resize is fine) that does not get triggered by a bunch of different actions as well?
(PS: why is there no vanilla event for this?!)
I'm looking for a way to run some JS when a textarea gets resized.
After an hour of searching and fiddling I have something that kinda works, but it is not good enough.
titleTextArea.mouseup(function() {
popup.update(); titleTextArea.focus();
});
The problem with the above code is that it also runs when clicking in the textarea. The code I am running causes the textarea to get re-rendered, which should not happen while someone is working in it, as it messes with the focus.
I've tried using jQuery resizable as per this SO post. For some reason the resize event does not fire. And really I'd prefer not needing to pull in jQuery UI for this.
Is there a way to run code on textbox resize (only on pletion of resize is fine) that does not get triggered by a bunch of different actions as well?
(PS: why is there no vanilla event for this?!)
Share Improve this question asked Nov 22, 2019 at 4:40 Jeroen De DauwJeroen De Dauw 10.9k16 gold badges60 silver badges82 bronze badges 3- Did you see this answer on that SO post? stackoverflow./a/7055239/1376624 It uses mouseup too, but might be smoother – Wesley Smith Commented Nov 22, 2019 at 4:47
- Unfortunately, resize event is consistently supported only for the whole window, or iframe. Learned that the hard way. – Andrei Kalantarian Commented Nov 22, 2019 at 4:47
- 1 Did you have a look at ResizeObserver API developer.mozilla/en-US/docs/Web/API/ResizeObserver? You can then implement a polyfill to support IE, Edge and Safari like here - codepen.io/florinsimion/pen/GRRLZyJ – Florin Simion Commented Nov 22, 2019 at 5:51
2 Answers
Reset to default 6A simple solution can be just adding a check for width/height, not a perfect solution though:
var ta = document.getElementById("text-area");
var width = ta.clientWidth, height = ta.clientHeight
document.getElementById("text-area").addEventListener("mouseup", function(){
if(ta.clientWidth != width || ta.clientHeight != height){
//do Something
console.log('resized');
}
width = ta.clientWidth;
height = ta.clientHeight;
});
<textarea id="text-area" rows="4" cols="50"></textarea>
Please try this.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="http://ajax.googleapis./ajax/libs/jqueryui/1.8.9/themes/base/jquery-ui.css"
type="text/css" media="all">
</head>
<body>
<textarea></textarea>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://code.jquery./ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$("textarea").resizable({
resize: function() {
alert("xxx");
}
});
</script>
</body>
</html>
本文标签: javascriptHTML textarea resize eventStack Overflow
版权声明:本文标题:javascript - HTML textarea resize event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745590854a2665187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论