Omar
Omar

Reputation: 67

Disabled scrolling by pressing spacebar, Then how to enable white spaces in textarea and ...?

I have disabled scrolling by pressing spacebar using code in-line(434)

And I use code in-line(427-432) to play/pause video by pressing spacebar anywhere in body.

// So how to enable white-spaces in textarea?

And how to disable playing/pausing video by pressing spacebar in textarea? //

I have tried code in-line(433) to enable white-spaces in textarea but it doesn't work.

https://i.sstatic.net/TEOTa.jpg

427-432:

Object.defineProperty(HTMLMediaElement.prototype, 'playing', { get: function() {

return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2); } });

$("body").on("keydown", function(e) { if (e.keyCode == 32) { if (document.querySelector("video").playing) { $("video")[0].pause(); } else { $("video")[0].play(); } } });

433:

$("#comment").on("keydown", function(e) { if (e.keyCode == 32) { return true; } });

434:

$(document).keydown(function(e) { if (e.which == 32) { return false; } });

Upvotes: 2

Views: 88

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 371049

In your document listener, only return false if the target of the event is not a textarea:

$(document).keydown(function(e) {
  if (e.which == 32 && e.target.tagName !== 'TEXTAREA') {
    return false;
  }
});
body {
  height: 2000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>

Or, to additionally permit spaces in inputs:

$(document).keydown(function(e) {
  if (e.which == 32 && !['TEXTAREA', 'INPUT'].includes(e.target.tagName)) {
    return false;
  }
});
body {
  height: 2000px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea></textarea>
<input>

Upvotes: 1

Related Questions