Juraj
Juraj

Reputation: 192

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);

Upvotes: 1

Views: 91

Answers (2)

Geek Num 88
Geek Num 88

Reputation: 5312

Try the wrap() method

$$('div.subresult a').each(function(item){
    var header = new Element('h3');
    item.wrap(header);

});

http://api.prototypejs.org/dom/Element/prototype/wrap/

Upvotes: 1

yethie
yethie

Reputation: 88

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);

Upvotes: 1

Related Questions