Reputation: 5
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
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
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
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
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