Question: $("#myTextArea").val() returns an empty string


$("#myTextArea").val() returns an empty string

Answers 1
Added at 2017-01-01 12:01

I'm having an issue where my textarea value is returning an empty string. I have a modal which opens on a dblclick event where there's a textarea and a button. I want that when the button is clicked the text written in the textarea is fetched and stored in a variable to be used in other functions. By the way when I click on the button the returned text is "".

the textarea html is:

<textarea id="text-comment" placeholder="Insert a new comment" style="min-width:100%"></textarea>

the button html is:

<button class="btn btn-default" data-dismiss="modal" id="edit">Edit</button>`

the function code is:

$(document).on('click', '#edit', function(){    //edits the selected note
    var classes = $(selectedNote).attr("class").toString().split(' ');
    var id = classes[2];
    var newText = $("#text-comment").val();
    console.log("new text is: "+newText);
    $(selectedNote).attr("title", newText);
    for(var i = 0; i < temporaryNotes.length; i++) {
        if(temporaryNotes[i]["ref"] == id) {
            temporaryNotes[i]["text"] = newText;
    for(var i = 0; i < notes.length; i++) {
        if(notes[i]["ref"] == id) {
            notes[i]["text"] = newText;
Answers to

$(&quot;#myTextArea&quot;).val() returns an empty string

nr: #1 dodano: 2017-01-01 12:01

I'm going to go on a limb here. The limb might break but who knows?

I believe your modal's contents are defined within the HTML itself. Perhaps you have <div class="modal-content"> or something - I don't know what modal plugin you're using (but if I ever write one, it will use <script type="text/html"> specifically to prevent this issue...)

The problem with defining modals in this way is that the "template" contents are themselves part of the document, even if the "base" one is never shown. The modal plug-in can then call cloneNode on the template and render that as the modal. Simple, right?

Not quite. IDs must be unique on the page, so any modal plug-in that uses cloneNode to render will end up with duplicate IDs all over the place.

To find out if this is the case, try running this code when your modal is visible on-screen:


This will show how many elements have that ID (whereas #text-comment may just stop after the first one). This value should be exactly 1. If it is 2 (or worse, more!) then you do indeed have a badly implemented modal plugin.

Without knowing exactly which plugin you're using nor how it works, I would suggest finding some way to uniquely identify the displayed modal as opposed to the template, and don't use IDs inside the template.

You'll need to use your browser's Developer Tools to do this, but as an example if your modal appears with class="modal-display" then you could do something like this:

var button = $(this),
    container = button.closest(".modal-display"),
    textarea = container.find("textarea"),
    value = textarea.val();

This kind of "relative search" for elements is much more flexible, and it will help you in future to learn this kind of thing. But for now, it should work around the issue of the duplicate IDs.

Source Show
◀ Wstecz