John August John August - 7 months ago 19
HTML Question

How to set text area display text based on Cookie value on page load?

I have a text area in the middle of my page like so:

<TEXTAREA NAME="carowner_notes" ROWS=6 COLS=25 ALIGN="center" value="decodeURIComponent('test%0D%0A%0D%0Atest')" onkeydown="check_length(50)" onkeyup="check_length(50)">
decodeURIComponent(test%0D%0A%0D%0Atest)
</TEXTAREA>


I am trying to set the display contents of the box to be the decoded string as the page loads. Is there an easy way to do this?

Right now my box looks like this:

enter image description here

I need it to look like this:

enter image description here

SOLUTION:

HTML:

<TEXTAREA NAME="carowner_notes" ROWS=6 COLS=25 ALIGN="center" value="decodeURIComponent('test%0D%0A%0D%0Atest')" onkeydown="check_length(50)" onkeyup="check_length(50)">
</TEXTAREA>
<SCRIPT>$("#carowner_notes").ready(function(){
document.getElementsByName('carowner_notes')[0].value = decodeURIComponent('test%0D%0A%0D%0Atest');
});
</SCRIPT>


I am using PL/SQL so this shows passing in the variable:

v_notes_cookieval varchar2 := 'test%0D%0A%0D%0Atest';
htp.formTextareaOpen('carowner_notes','6', '25', 'center', 'value="decodeURIComponent('''||v_notes_cookieval||''')" onkeydown="check_length(50)" onkeyup="check_length(50)"');
htp.formTextareaClose;
htp.script('$("#carowner_notes").ready(function(){
document.getElementsByName(''carowner_notes'')[0].value = decodeURIComponent('''||v_notes_cookieval||''');
});');

Answer

You can run this code on page load.

var textarea = document.querySelectorAll('textarea')[0]

textarea.value = decodeURIComponent('test%0D%0A%0D%0Atest')

Obviously, you'll probably want to assign an ID to the TEXTAREA node in the event that you have more than one TEXTAREA node.

Also, the 'test%0D%0A%0D%0Atest' doesn't have to be hardcoded. You can pull that from the query/hash of the URL, or elsewhere.