views:

370

answers:

2

I'm trying to build some JavaScript in MooTools that fetches table row markup from the server, then adds it to a table. I have an object named htmlAjax that's set as a MooTools Ajax object. The object gets the HTML from the server correctly (tested this in the Firebug console), but when I attempt to add this markup to a new element, the <td> elements get stripped out. Here is my function that captures the data from the HTTP request and attempts to add a row:

htmlAjax.addEvent('onComplete', function (data) {
    var slot = new Element('tr');
    slot.setHTML(data);
    slot.injectInside($('volunteer_slots'));
});

The response I get back from the server in data looks like this:

<td><div class="title" rel="60">
    <a class="modal" href="index.php?view=volunteerslot&amp;tmpl=component&amp;id=60" rel="{handler: 'iframe', size: {x: 650, y: 550}}" >test</a>
</div>
</td>
<td class="button_column">
    <img src="images/publish_x.png" />
</td>
<td>0 minutes</td>
<td><select name="volunteers[60]" id="volunteers60" class="inputbox"><option value="0"  selected="selected">(select...)</option><option value="63" >Joe LeBlanc</option><option value="64" >Test User</option><option value="65" >Test User</option><option value="66" >Test User</option><option value="67" >Test User</option></select></td>
<td><select name="total_time_spent[60][hours]" id="total_time_spent60hours" ><option value="0"  selected="selected">0</option><option value="1" >1</option><option value="2" >2</option><option value="3" >3</option><option value="4" >4</option><option value="5" >5</option><option value="6" >6</option><option value="7" >7</option><option value="8" >8</option><option value="9" >9</option><option value="10" >10</option><option value="11" >11</option><option value="12" >12</option><option value="13" >13</option><option value="14" >14</option><option value="15" >15</option><option value="16" >16</option><option value="17" >17</option><option value="18" >18</option><option value="19" >19</option><option value="20" >20</option><option value="21" >21</option><option value="22" >22</option><option value="23" >23</option><option value="24" >24</option></select>&nbsp;hours&nbsp;&nbsp;<select name="total_time_spent[60][minutes]" id="total_time_spent60minutes" ><option value="0"  selected="selected">0</option><option value="15" >15</option><option value="30" >30</option><option value="45" >45</option></select>&nbsp;minutes</td>
<td class="button_column">
    <img src="images/tick.png" />
</td>

Once I call setHTML on the slot variable (passing in data), the <tbody> element volunteer_slots has a new row, but that new row contains everything except for the <td> elements. Is there any way to prevent this from happening?

A: 

What version of MooTools are you using? I'm not sure if it has anything to do with that, but in the latest version, there is no (as far as I'm aware) setHTML method, it's now set('html', data). I also believe that injectInside has become slot.inject($('volunteer_slots')). Hope this helps.

theIV
+2  A: 

This is quite interesting. While I can't explain it, I do have a solution for you. Reverse the order of your setHTML() and injectInside() lines, and the <td>s are inserted properly:

slot.injectInside($('volunteer_slots'));
slot.setHTML(data);

You are apparently using MooTools 1.11, however, this behaviour appears to occur in 1.2 as well. Very odd! It must have something to do with the fact that before the slots var is injected, it is not part of the DOM. The effects of setting the innerHTML of a non-DOM element might be unexpected? I wish I could explain it. I'll have to dig deeper when I have time.

zombat
Works, thanks! And yes, I am using 1.11.
jlleblanc
good catch this. mootools does sometimes do odd things with elements it injects, I have had it mess up style from block to inline on elements injected into a div with overflow...
Dimitar Christoff