#161 doc
Andrew Plotkin

Element.insert/update should accept DOM nodes

Reported by Andrew Plotkin | June 11th, 2008 @ 09:26 PM | in Inline documentation

I'm not sure that's the right solution to my problem, but it's the solution which is most consistent with the Prototype docs at present.

I'm writing a function which takes some text and puts it into an Element. I'd think this would be a very common operation, but Prototype balks. I try many things:

  el.textContent = str;

...works in webkit/firefox, but not konqueror 3 or IE6.

  el.update(str);

...parses str as HTML, which is not what I want at all. (If str contains "<" signs, the page comes out broken and possibly a security hole.) </p> <pre><code class="javascript"> el.update(str.escapeHTML()); </code></pre> <p> ...works, but then every piece of text that flows through my app is being escaped and then unescaped, which seems painfully inefficient. </p> <pre><code class="javascript"> el.update(document.createTextNode(str)); </code></pre> <p> ...inserts "[object Text]" rather than my string. </p> <pre><code class="javascript"> el.update().appendChild(document.createTextNode(str)); </code></pre> <p> ...works, by virtue of avoiding the Prototype API as much as possible. :) </p><p> I'd like a clean and supported way to do this inside Prototype. Accepting DOM text nodes in update() seems like the tidiest option. </p><p> This goes for Element.insert() as well. The docs say it accepts a "DOM element", but the code </p> <pre><code class="javascript"> el.insert(document.createTextNode(str)); </code></pre> <p> ...causes an "undefined" error inside Prototype.

Comments and changes to this ticket

  • Andrew Plotkin

    Andrew Plotkin June 11th, 2008 @ 09:28 PM

    Ack! I appear to have destroyed the formatting of the bug. Sorry! I can refile it without the offending LESS-THAN-SIGN if you want.

  • Juriy Zaytsev

    Juriy Zaytsev June 11th, 2008 @ 10:03 PM

    Interesting.

    So why can't you just crete an element helper:

    Element.addMethods({
      updateText: function(element, text) {
        element = $(element);
        element.update().appendChild(document.createTextNode(text));
        return element;
      }
    })
    
    ...
    
    $('someElement').updateText('someText');
    
  • Andrew Plotkin

    Andrew Plotkin June 11th, 2008 @ 11:07 PM

    That is just what I will do, tonight, for this version of my project using Prototype 1.6.0.2.

    But this seems like a feature within Prototype's sphere of competence. I spent a lot of time going through various options, figuring out why each one didn't work (until I came up with the last one). Plus, as I said, the docs for Element.insert() are at best misleading.

  • Juriy Zaytsev

    Juriy Zaytsev June 11th, 2008 @ 11:45 PM

    • → Assigned user changed from “” to “Andrew Dupont”
    • → Milestone changed from “” to “Inline documentation”
    • → State changed from “new” to “doc”

    Thanks, Andrew.

    I'll mark it as a documentation issue.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket