Jquery - Getting what actually is displayed in in text area

We are automating web tests and want to validate what is in the text area.

We want to check what is actually showing from a user perspective. It does not matter if it's placed there with placeholder or entered by user/testcase. We use the html5 placeholder attribute.

$("textarea").text() does not give me anything if the text has been entered via place holder.

Tried $("textarea").val() but it gives me the same behavior as using .text().

We could write logic like if ( text is empty ) { look at placeholder } but that feels wrong since it would not determine what is actually showing in the text area. I would like to have an alternative to using the logic. Otherwise we assume that the browser uses the same logic that we implement in our test.

Any ideas?

Upvotes: 0

Views: 68

Answers (2)

Rain Diao
Rain Diao

Reputation: 926

you mean html5 placeholder or placeholder added by js plugin?

if is case 1, i guess this can help:

var text = $("textarea").val() || $("textarea").attr('placeholder')

Demo: http://jsfiddle.net/mJkFc/

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Try this:

var t = document.getElementsByTagName('textarea')[0];
var text = t.value || t.getAttribute("placeholder") || "";

Upvotes: 0

Related Questions