Pergunta

There are several textareas on my page and I need the text of the one someone is clicking on. I would know how to do it in jQuery but I need a prototype/javascript solution. What I tried so far:

$$("textarea").each(function (el) {
      el.observe('click', respondToClick);
      function respondToClick(event) {
         var element = Event.element(event);
         var text = element.innerHTML();
         console.log(text);
      }
});

There are no errors in the console but also not the text I need. So how to get it?

EDIT: That's the solution. I forgot the document.ready-equivalent in prototype and thanks to bruchowski I could edit the correct prototype method to get the text:

document.observe("dom:loaded", function () {
     $$("textarea").each(function (el) {
          el.observe('click', respondToClick);
          function respondToClick(event) {
              var element = Event.element(event);
              var text = element.value;
              console.log(text);
          }
     });
});

P.S.: Prototype Vesion is 1.7.0

Foi útil?

Solução

You can use Form.Element.getValue(), or its $F() alias:

 var element = Event.element(event);
 var text = $F(element);
 console.log(text);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top