views:

17

answers:

2

I know this can be done easily using jQuery but haven't been able to find out how to accomplish it with YUI 2.

I am submitting a form via AJAX and in the callback, on success, I want to add the information that was inserted into the existing table in the DOM.

This is what I am trying with no success:

var callback = {
    success: function(o) {
        var result = YAHOO.lang.JSON.parse(o.responseText);
        if (result.success == true) {
            var last_row = YAHOO.util.Dom.getLastChild('tbody_urls');

            var html = '<tr><th>' + result.name 
                + '</th><td><a href="' + result.url + '" title="' + result.url + '">' + result.url.substr(0, 80) + '</a></td><td></td><td></td></tr>';

            var el = YAHOO.util.Dom.insertAfter(html, last_row); // el is being set to NULL so the insert is failing

            YAHOO.util.Dom.get('form_urls').reset();
        }
    },
    failure: function(o) { error(); }
};
+1  A: 

You can't insert an html string after a node. Try to create a row element, insert the html inside it and append the row element:

var callback = {
    success: function(o) {
        var result = YAHOO.lang.JSON.parse(o.responseText);
        if (result.success == true) {
            var last_row = YAHOO.util.Dom.getLastChild('tbody_urls');

            var html = '<th>' + result.name 
                + '</th><td><a href="' + result.url + '" title="' + result.url + '">' + result.url.substr(0, 80) + '</a></td><td></td><td></td>';
            var row=document.createElement("tr");
            row.innerHTML=html;

            var el = YAHOO.util.Dom.insertAfter(row, last_row); // el is being set to NULL so the insert is failing

            YAHOO.util.Dom.get('form_urls').reset();
        }
    },
    failure: function(o) { error(); }
};

UPDATE create a dom element for every td and th

var callback = {
    success: function(o) {
        var result = YAHOO.lang.JSON.parse(o.responseText);
        if (result.success == true) {
            var last_row = YAHOO.util.Dom.getLastChild('tbody_urls');

            var row=document.createElement("tr");
            var th=document.createElement("th");
            th.innerHTML=result.name;
            row.appendChild(th);
            var td=document.createElement("td");
            td.innerHTML='<a href="' + result.url + '" title="' + result.url + '">';
            row.appendChild(td);
            row.appendChild(document.createElement("td"));
            row.appendChild(document.createElement("td"));

            var el = YAHOO.util.Dom.insertAfter(row, last_row); // el is being set to NULL so the insert is failing

            YAHOO.util.Dom.get('form_urls').reset();
        }
    },
    failure: function(o) { error(); }
};
mck89
That's closer. It's inserting but the th and td tags are stripped out for some reason. ie. <tr>Lease<a title="http://www.url.com" href="http://www.url.com">http://www.url.com</a></tr>
Matt McCormick
Try with the other solution that i've just written
mck89
Thanks. That works. What a pain though. I thought there would have been an easier way.
Matt McCormick
A: 

Better to use appendChild because insertAfter will not work if there are no existing tr's.

<snip>
var tbody = YAHOO.util.Dom.get('tbody_urls');
tbody.appendChild(row);
Matt McCormick