javascript - HTML textarea resize event - Stack Overflow

I'm looking for a way to run some JS when a textarea gets resized.After an hour of searching and f

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
Add a ment  | 

2 Answers 2

Reset to default 6

A 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745590854a4634821.html

相关推荐

  • javascript - HTML textarea resize event - Stack Overflow

    I'm looking for a way to run some JS when a textarea gets resized.After an hour of searching and f

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信