yogsma
yogsma

Reputation: 10594

How to find/detect any textarea in page in javascript

(document.getElementById('textarea').length > 0) doesn't work. Does anyone know anything else other than this?

Will

Here is the scenario from my previous question which was unanswered. I have Rich text Editor(Openwysiwyg) which is loaded into textarea when I go to that particular page where textarea is placed. The function uses textarea id to identify textarea to replace it with Rich Text Editor(RTE). Now the script to call this function is in header part of the page. I select a drop-down option for sending email, so my textarea for email shows up. With this script added for RTE, my textarea for email is replaced by RTE and I can send formatted emails. So this works perfectly fine in Firefox. With IE7, RTE shows up even before I select drop-down option for email and this makes whole page messed up.When I select drop-down option for email, I just see normal text area and RTE still sitting at top of page.

Upvotes: 4

Views: 7748

Answers (4)

Zaje
Zaje

Reputation: 2309

document.getElementsByTagName('textarea').length > 0

Upvotes: 12

Ankit Parmar
Ankit Parmar

Reputation: 788

Pure JavaScript (You can use this code)

textarea is HTML element tag

JavaScript :

if(document.getElementsByTagName('textarea').length > 0) {

}

HTML CODE:

<div class="flavor">

  <div class="value">

    <textarea name="name" rows="8" cols="80"></textarea>

  </div>

</div>

<script type="text/javascript">

  var flavorbox = document.getElementsByClassName('flavor')[0]  
                          .getElementsByClassName('value')[0]
                          .getElementsByTagName('textarea').length;

  alert(flavorbox);

</script> 

Upvotes: 1

adardesign
adardesign

Reputation: 35781

Since openWYSIWYG generates a iframe on the fly, its not so simple to get/set its content.

I am currently working on changing these settings in the source. will post here a link as soon as i get the changes.

Upvotes: 0

elcuco
elcuco

Reputation: 9208

You can use (note the plural form!)

var e = document.getElementsByTagName("textarea");

You can use jQuery as well:

$("textarea").each( function() { /* ... */ } );

EDIT: I faced a similar problem once. I was using fckedit, and when I tried reading the value of my textedit (document.getElementById('blabla').value) I was getting null, even tough the rich text edit was ddefinetly showing something on screen.

It turns out that the fckedit API opens a new element on top of the textearea, and only when you navigate from the page is syncs it's internal data (which is on an iframe, if I am not mistaking) into the original textarea.

The moral of the story: if you are using some richtext API - use it's API to query the status of your "textarea". Hope this helps you, as I don't know the library you are using.

PS: I actually used $("blabla").val() ... which is also JavaScript... for some reason people think that jQuery is not javascript. Why is that?

Upvotes: 3

Related Questions