3

I have a page which dynamically brings in a form via ajax and displays it in a modal div (one that sits above an overlay div that covers the entire page). This is to let them save certain data before a window closes. Everything works great except one thing.

$('#save_close_form').find('*[name]').each(function(index, form_element) {
    var cfe = (form_element.jquery == undefined ? $(form_element) : form_element);
    console.log(cfe.attr('name') + " => " + cfe.attr('value'));
    if (cfe.attr('name').match(/data\[/)) {
        if (cfe.attr('type') == 'checkbox') {
            if (cfe.attr('checked')) {
                map[cfe.attr('name')] = 'on';
            }
            else {
                map[cfe.attr('name')] = '';
            }
        }
        else if (cfe.attr('type') == 'radio') {
            // only get checked radio buttons
            if (cfe.attr('checked')) {
                map[cfe.attr('name')] = cfe.attr('value');
            }
        }
        else {
            map[cfe.attr('name')] = cfe.attr('value');
        }
    }
});

The part in the else {} clause at the end triggers for TextArea and input type="text" elements, but for some reason it always sees cfe.attr('value'); as undefined for the TextArea. I'm using FF6.0 with jQuery 1.6 for this.

Derek
  • 4,575
  • 3
  • 22
  • 36
  • 2
    Why are you not using `cfe.val()`? – Tejs Aug 24 '11 at 18:49
  • in a previous version of this code i tried that and it never worked. the weird thing is that the above code was working just fine until recently when i upgraded JQ from 1.3 to 1.6 – Derek Aug 24 '11 at 18:52

6 Answers6

14

Try .val() instead of .attr('value').

<textarea> doesn't have a value attribute (the text is between the tags, not in value="") however I believe .val() will retrieve it.

JJ.
  • 5,425
  • 3
  • 26
  • 31
  • I guess in 1.6 they fixed the way .val() works in textareas, as it now seems to work with that. The strange thing is that .attr('value') was working fine until now. – Derek Aug 24 '11 at 19:06
2

For textareas use :

 $("#textareaid").val() or $("#textareaid").html()

instead.

jQuery get textarea text

Set value of textarea in jQuery

Community
  • 1
  • 1
AJC
  • 1,853
  • 1
  • 17
  • 28
1

TextArea does not have an attribute called value...try using val

map[cfe.attr('name')] = cfe.val();
legendofawesomeness
  • 2,901
  • 2
  • 19
  • 32
1

textarea does not have a value attribute by default. You should use cfe.val() or cfe.html() to get its content.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
0

It's because a textarea doesn't have a value attribute.

Use .val() instead.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

Ok, so the reason is that jQuery 1.6 makes a distinction between the attribute at creation and your current property value. You've created the textarea without a value attribute more than likely; thus, the initial value is undefined. When you want to pull the defined value, you need to either use .prop('value'), or call the more helpful .val() method.

Tejs
  • 40,736
  • 10
  • 68
  • 86