1

I have an automatically generated textarea with a class name, say "x". How do I get the text inside the text area given that there will be only one element with the class name "x" in that page.

I have tried the following and can't make it to work.

<textarea class="note-codable" role="textbox" aria-multiline="true" style="height: 390px;">
 some content 
</textarea>

<script>
$(document).ready(function () {
  alert($('.note-codable').val());
  alert($('.note-codable').first.val());
  alert($('.note-codable').text());
})

also how do i update the same text area? any help is appreciated. thanks in advance.

Arun Laxman
  • 376
  • 2
  • 15

3 Answers3

2

You Can Use:

$(selector).text() i.e

$('.x').text();
Aayush
  • 409
  • 4
  • 17
1

use .val(). Its working fine

$(document).ready(function () {
  console.log(document.querySelector('.note-codable'))
  alert($('.note-codable').val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="note-codable" role="textbox" aria-multiline="true" style="height: 390px;">
 some content 
</textarea>
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Hi, I have edited my question now. It still doesn't work for me. Actually I'm trying to update content to a rich text editor. there is this role="textbox" after the class name. is this what causing failure? – Arun Laxman Feb 15 '19 at 09:17
  • Yes. I know, I think it has something to do with the rich text editor i'm trying to play with. – Arun Laxman Feb 15 '19 at 09:39
  • I'm Using summernote RTE with Django. – Arun Laxman Feb 15 '19 at 09:47
  • @ArunLaxman buddy i just saw https://summernote.org/. Go and **inspect element** the the RTE. You will see that ` – Maheer Ali Feb 15 '19 at 10:03
  • I'm really grateful to you for going to such extreme lengths. but there's a textarea for codeview. if you inspect the codeview of Summernote, you get a textarea with note-codable class name. – Arun Laxman Feb 15 '19 at 10:06
1

check below code for this:

$(document).ready(function () {
  alert($('.x:first').html());
})

Hope it helps you.

Rohit Mittal
  • 2,064
  • 2
  • 8
  • 18
  • That's actually misleading. Although a textarea does not have a value _attribute_, it still exposes a value _property_ to be accessed with JavaScript – Psi Feb 22 '19 at 09:59
  • Thanks for correcting me and I have updated my comment which was misleading @Psi – Rohit Mittal Feb 22 '19 at 10:06