Jump to content

Hi,

 

Very confused by this, I've got this code:

<div class="col">
	<textarea id="post-reply-textarea" style="-snip-" placeholder="Enter your reply..."></textarea>
</div>

Then trying to get the value from it using jquery:

$("#post-reply-textarea").get(0).value;

Also tried with base JS:

document.getElementById("post-reply-textarea").value;

Both return empty:

''

Also tried innerHTML and val() for jquery, same result.

 

If I initialise the textarea first like this:

<div class="col">
	<textarea id="post-reply-textarea" style="-snip-" placeholder="Enter your reply...">Some text</textarea>
</div>

Then it works. Although, any edits that I make in the textarea are ignored.

 

I'm running these in the Console in inspect element so there shouldn't be any issues with the text area not being loaded or anything like that. Why am I getting this result and how do I fix it?

Cheers.

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to comment
https://linustechtips.com/topic/1504923-js-textarea-value-is-always-empty/
Share on other sites

Link to post
Share on other sites

Check that you're actually getting a return value of the DOM object itself.

 

console.log($('#post-reply-textarea'));

It should be a jQuery Object with length equal to 1.  It won't have a length greater than one because it's an ID selector, so you may want to check to make sure you've not accidentally used that ID more than once.  Just search your source markup for multiple occurrences of the ID.  If it's an object with length 0, then you need to change which document is the active one in the inspector.  This can be caused if you've used iframes.  I ran a quick test with jQuery pulled from a CDN and each of your access methods work to obtain the value of the textarea with the exception of innerHTML.  To change the active document in Firefox (on a page with iframes), there will be a dropdown near the top left corner of the console.  Change its value to a page you expect the textarea to be on.  A similar process exists in Chrome.  The dropdown is near to the clear console button, which looks like a circle with a diagonal line through it.

Link to post
Share on other sites

12 hours ago, Vicarian said:

Check that you're actually getting a return value of the DOM object itself.

 

console.log($('#post-reply-textarea'));

It should be a jQuery Object with length equal to 1.  It won't have a length greater than one because it's an ID selector, so you may want to check to make sure you've not accidentally used that ID more than once.  Just search your source markup for multiple occurrences of the ID.  If it's an object with length 0, then you need to change which document is the active one in the inspector.  This can be caused if you've used iframes.  I ran a quick test with jQuery pulled from a CDN and each of your access methods work to obtain the value of the textarea with the exception of innerHTML.  To change the active document in Firefox (on a page with iframes), there will be a dropdown near the top left corner of the console.  Change its value to a page you expect the textarea to be on.  A similar process exists in Chrome.  The dropdown is near to the clear console button, which looks like a circle with a diagonal line through it.

The problem was a bit finnicky, but it's exactly what you said. I did have two elements with the same ID, the problem was that one was being created on start-up, this was supposed to replace the existing textarea but I forgot to delete the original one, so I ended up with 2 and hence the problem. Deleting the original textarea fixed the problem 😌

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×