Search code examples
javascriptprototypejs

Trying to add a header around an anchor with javascript prototype


I'm trying to place headers around a couple of anchors with Prototype. But it doesn't work? Can someone help me with this? Thanks.

            function placeheader(item, i)
            {
                item.insert({
                    //This doesnt work:  
                    before: "<h3>",
                    after: "</h3>"

                    //This works:           
                    //top: "test3",
                    //bottom: "test4"
                }); 
            }               
            $$('div.subresult a').each(placeheader);

Solution

  • I'm afraid you cannot use the insert() method with partial tags, try this:

    function placeheader(item) {
        var header = $(document.createElement("h3"));
        item.up().insertBefore(header, item);
        item.remove();
        header.appendChild(item);
    }
    $$("div.subresult a").each(placeheader);