1

I have this sample code:

window.addEventListener('load', () => {
  // sticky profile notes
  const notes = document.querySelector('#id_notes'); // Get textarea element
  const headerDiv = document.createElement("div"); // Create a <div> element
  headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions
  // add header on top 
  notes.prepend(headerDiv);
});
<body>
  <textarea id="id_notes" style="background-color: #ffffcc;">
    </textarea>
</body>

in which I plan to add a header the same size as the textarea. but instead of it being added on top of the textarea, its being added inside it.

enter image description here I can only use JS because I don't know how to add it on a Django template. Thanks in advance!

EDIT: added desired end result:

enter image description here

2
  • I don't think you can put divs inside a textarea as it is a replaced element. Commented Jul 18, 2021 at 20:28
  • 1
    @evolutionxbox, I added a desired end result. I wasn't planning on adding a div inside a text area in the first place Commented Jul 18, 2021 at 20:30

2 Answers 2

2

You can use Node#insertBefore:

window.addEventListener('load', () => {
  // sticky profile notes
  const notes = document.querySelector('#id_notes'); // Get textarea element
  const headerDiv = document.createElement("div"); // Create a <div> element
  headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions
  // add header on top 
  notes.parentNode.insertBefore(headerDiv, notes);
});
<textarea id="id_notes" style="background-color: #ffffcc;"></textarea>

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

Comments

1

Use insertBefore() instead

window.addEventListener('load', () => {
 
  const notes = document.querySelector('#id_notes'); // Get textarea element
  
  const headerDiv = document.createElement("div"); // Create a <div> element
  headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions
  
  // add header on top 
  notes.parentNode.insertBefore(headerDiv, notes)
});
<body>
  <textarea id="id_notes" style="background-color: #ffffcc;">
    </textarea>
</body>

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.