最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - HTML textarea resize event - Stack Overflow

matteradmin7PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far