1

all. I'm curious to know if the regular text input (not text area) could detect multiple lines value. If I copy any string that has various lines and pastes it into a standard input box, it will display as a single-line string and has no idea whether it has multiple lines or a single line.

I just want to know if we can preserve the original value (with multiple lines)

Original string value

INVOICE_500
INVOICE_501

After pasting it into the regular text input

INVOICE_500 INVOICE_501

Thanks in advance.

1
  • the regular input type=text is one line only. All you can do is add a paste event listener so that anytime the value will change because of pasting, it will be stored inside a data attribute so you will preserve the original value. Commented Dec 28, 2022 at 9:15

2 Answers 2

1

I found an article from a previous post where an way has been shown. We can use clipboard API as it copy text as it was written. It was applied on a div. I have converted it on a input field. I have attached the code below. Hope this might help you.

Reference article: Clipboard data on paste event

function handlePaste(e) {
  var clipboardData, pastedData;

  // Stop data actually being pasted into input
  e.stopPropagation();
  e.preventDefault();

  // Get pasted data via clipboard API
  clipboardData = e.clipboardData || window.clipboardData;
  pastedData = clipboardData.getData('Text');

  // Do whatever with pasteddata
  alert(pastedData);
}

document.getElementById('pasteInput').addEventListener('paste', handlePaste);
<input id='pasteInput' placeholder='Paste text here'></div>

Sign up to request clarification or add additional context in comments.

Comments

0

No you can't, the only HTML input element that's designed to be multi-line is the "textarea" element.

<textarea name="textArea" cols="40" rows="5">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.