views:

284

answers:

3

I'm trying to add a table row with ajax/jquery that has a form element in it. Everything works just fine if I set it without the ajax, but somehow everything inside the <form> tag is just completely lost.

I'm not sure where I'm losing the form (jquery's .html() is effectively the same as innerHTML right? If that's the case I suspect that's where I'm losing it).

Anyway, here's some code:

var worow = document.getElementById('worow_' + row);
var wotable = document.getElementById('tbl_workorders');

// add a new row to the table underneath our existing row.
var newrow = wotable.insertRow(worow.rowIndex+1);
var x = newrow.insertCell(0);

// set up the row a little bit
x.colSpan = 13;
x.style.padding = '10px';
x.style.backgroundColor = '#ccc';
x.align = "center";

x.innerHTML = '<img src="/images/loading.gif" />';

// a little ajax cuz we're cool that way
$.post("getwotrans.php", 
{
    workorder: row
}, 
function(response)
{

    // set the value of the row = response object from the AJAX
    $(x).html(response);
});

And in getwotrans.php: (paraphrased)

<table>
<thead><tr><td>blahblah</td></tr></thead>
<tbody><form><tr><td><input></td></tr></form></tbody>
</table>

So what happens is I'll run the javascript function to add the row, and the row is added fine and I see the table headers, but the 'form' inside the tbody is just not there.

A: 

What happens when you put the form outside of the table?

<form><table>
<thead><tr><td>blahblah</td></tr></thead>
<tbody><tr><td><input></td></tr></tbody>
</table></form>

Just curious if this will fix the issue or not? It is odd that this would happen without something equally odd to fix it!

Andrew Siemer
I can indeed put it outside the table, but that's not really the functionality I was going for. The goal here is inline row editing, so it's really easy to just put a separate form around each row (as opposed to using a hidden field or some other means of figuring out which row we're editing).
epalla
A: 

a form cannot be a child element of tbody

redsquare
I understand it's not technically correct. But it works in FF, Chrome and IE until I try to put it there with a little ajax. Are you saying javascript won't let me put a form inside a tbody?
epalla
jquery needs a valid dom to work. It should validate at w3c, if not expect inconsistencies across browsers. Why not wrap the table in a form?
redsquare
It's not inconsistent across browsers. It consistently works in all browsers I've tried UNTIL I try to put it through ajax. Are you saying if an AJAX response doesn't validate, jquery will ignore it?
epalla
Whats wrong with just complying and making sure the markup is valid?
redsquare
Agreed. You're saying "it doesn't work right" and the answer is "of course not, you're doing it wrong". The fact that it works in one situation but not the rest is inconsequential.
Some Canuck
I'm not concerned with a dogmatic dedication to valid markup. Unless you're telling me invalid markup will cause an ajax request to fail or act unexpectedly, you're not telling me anything.
epalla
Why ask a question if your going to refute an answer. Its like saying I'm going to buy that petrol car but I'm going to fill it with water and fingers crossed....enjoy hacking around forever more to get things to work!
redsquare
it wasn't an answer to the question. The question is about an odd ajax response, you answered with validation issues. I never rejected it outright - I asked for clarification as to whether valid markup would cause an ajax request to act unexpectedly and have not gotten a straight answer from anyone. All you seem to see is invalid markup and the rest of the question just disappears.
epalla
I bet you the ajax response works if you dont have the invalid markup. How is that not answering the question? Did you even try it? I guess not otherwise you would stop being so pig headed and accept that valid markup helps jquery perform correctly.
redsquare
+1  A: 

Mhh i had some simliar problem. I used a hidden form and javascript to copy the values of the row clicked to the hidden form elements and then submit the form via javascript. Maybe thats an idea.

Richard
This is the solution I went with, though I don't consider it a solution as much as a workaround.
epalla