Populating cftextarea richtext with Javascript

On February 11, 2009, in ColdFusion, Javascript, by Anuj Gakhar

I had a cftextarea with richtext enabled and I had another dropdown which was supposed to change the value of the cftextarea when selected. So, I was doing it the old way as I would have imagined.

var msgbox = document.getElementById("mytextarea");
msgbox.innerHTML = 'New Value';

But that was not working. And after spending some time trying to get this done, I finally struck the solution :-

var msgbox = document.forms['myForm'].mytextarea.id;
ColdFusion.RichText.setValue(msgbox, 'New Value');

The trick here is that the id of the cftextarea is dynamically generated by CF during run-time, so you need to get the element by the name and then get it’s id. And then use ColdFusion.RichText.setValue() function from cfrichtexteditor.js .

I hope this little tip helps someone out there.

Tagged with:  

5 Responses to Populating cftextarea richtext with Javascript

  1. Sebastiaan says:

    This would work for all CFAJAX objects, like cflayoutarea et al.

    Still wonder why though this ID is created in the fly and not set the same as the name – which in Webstandards terms would be the correct way of doing it.

  2. Sebastiaan says:

    Hmm, I was expecting your blog to fail my Captcha… As I couldn’t possible discern what the extreme long second word was, I just typed in “something”. And then my comment was posted! Shouldn’t it have failed me? At least that’s how I as a user expect a Captcha to work.

  3. Anuj Gakhar says:

    Hi Sebastiaan, I think the ID thing is to avoid any duplicate elements in the DOM, which makes sense. And this captcha sometimes gets a bit lenient, which is a good thing 🙂 May be you were recognized as a trusted user 🙂 On a serious note, i will find out why it didnt fail when it should. thanks for letting me know.

  4. Sebastiaan says:

    Second time around it failed me initially (another unreadable Captcha) but the second time it let my comment pass even though I actually got it wrong the second time as well…

    I cannot for my life read this one either so this is another test 😉

    About the ID’s: good that CF wants the DOM to stay intact, but it’s not good for me wanting to address the CFGRID ID either via JS (your solution does work however) or via CSS. I had this issue wanting to style the grid a certain way, which would override the default EXT framework CSS – alas it didn’t work. And being on a shared server I cannot possibly change the root-files 🙁

  5. Flüge says:

    I would have done the same thing and never thought to use the Dom to obtain te ID from the generated textarea.

    [var msgbox = document.getElementById(“mytextarea”);
    msgbox.innerHTML = ‘New Value’;]

    IT is really important to know that
    Coldfusion generates the Ids of the cftextarea dynamically even if u specify the Id.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 446 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: