views:

48

answers:

2

Hello

I'm studying a strange behavior in Google Chrome when using jQuery text function:

$(document).ready(function () {
   $("#btnSubmit").click(function () {
       var t = $('#txtMessage').text();
       alert(t); //this shows nothing in Google Chrome, but works in IE9
   });
});

when I change var t = $('#txtMessage').text(); to var t = $('#txtMessage').val(); it works in Chrome.

So, what's wrong with text() function?

PS: txtMessage is a textarea

+1  A: 

The .val() function should be used to get the value of form elements like input, select, textarea, ...

Darin Dimitrov
+3  A: 
  • .text() gets the inner text of an element, so when getting the value it's just not an appropriate usage.
  • .val() gets the value property of an input type element, and should be what you use to retrieve/set the value (this includes <textarea> elements, even though the value is between the tags).

The question isn't really "why doesn't .text() work in Chrome?", it's "why does .text() in IE?". Stick with .val() for inputs.

Nick Craver
sure, I have being using val() by now
Junior Mayhé
I've posted it to Microsoft Connect https://connect.microsoft.com/IE/feedback/details/618388/jquery-1-4-3-text-behavior-different-when-compared-to-chrome
Junior Mayhé