Nurbek
Nurbek

Reputation: 5

Get textarea value from textarea object

Why doesn't this work?. Here the input type is text:

var name = $("input[name='Event[name]']").serializeArray();
name = name[0].value;

var description = $("input[name='Event[description]']").serializeArray();
description = description[0].value; 

When I want to get the from a textarea instead, it doesn't work.

Upvotes: 1

Views: 347

Answers (5)

Roy M J
Roy M J

Reputation: 6938

Is there any particular reason as to why you use get value as array?

If not see below :

var name    = $('#name').val();
var description = $('textarea#description').val();

considering name and description is what you have given as id for text field and text area.

Upvotes: 0

jerone
jerone

Reputation: 16861

This should work:

var name = $("input[name='Event[name]']").val();

var description = $("input[name='Event[description]']").val();

Let jQuery handle value.

The .val() method is primarily used to get the values of form elements such as input, select and textarea.

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328546

A textarea as no value attribute. Use $("input[name='Event[description]']").val() since jQuery folds all values for all kind of input elements (input, select, checkbox and textarea) into this function call.

That means you should always use .val() to get the value for anything - your code will be more simple and you can even change the type of an input element without breaking anything.

Upvotes: 0

Craig
Craig

Reputation: 4399

value isn't a property of a textarea. Textarea's are nodes that have content. Use the JQuery attribute for textContent.

Upvotes: 0

Amit
Amit

Reputation: 15387

Replace your code as .val() to .text()

Upvotes: 0

Related Questions