views:

69

answers:

3

Hello...This code produces a mess... What am i doing wrong???

 cell=$("<td>");
        if(normal.exam_type=="Exam_Boolean")
        {
            var input=cell.append("<input>").last();

            input.attr("type","hidden");
            input.attr("name","exam.exam_Normal['" +normal_id_unique + "'].boolean_v");
            input.attr("value",normal.normal_boolean);               
A: 

Rather than using cell.append(input) - which returns a reference to cell - try doing it "the other way around" as input.appendTo(cell):

var cell = $("<td>");
if(normal.exam_type=="Exam_Boolean")
{
    var input= $("<input />").appendTo(cell);
    input.attr("type", "hidden");
    input.attr("name", "exam.exam_Normal['" + normal_id_unique + "'].boolean_v");
    input.attr("value", normal.normal_boolean);
);
Jørn Schou-Rode
Good idea i will try
Parhs
gives me a jquery exception :(
Parhs
@Parhs: Why, where? It looks ok to me.
Jørn Schou-Rode
At the jquery.js file...But i fix the problem ifvar input= $("<input>").appendTo(cell); input.attr("type", "hidden");var input= $("<input>"); input.attr("type", "hidden");input.appendTO(cell);
Parhs
That does not seem to make much sense. Closing the `<input />` element might, though, as shown in the updated answer. Anyhow, I see you have found your solution, so I will not bugger you anymore :)
Jørn Schou-Rode
+1  A: 

I'd do it this way:

var cell = $("<td></td>");
if(normal.exam_type=="Exam_Boolean")
{
    $("<input />").attr("type", "hidden")
          .attr("name", "exam.exam_Normal['" +normal_id_unique + "'].boolean_v")
          .attr("value", normal.normal_boolean)
          .appendTo(cell);
);

[newlines before dots are just for readability.]

You might also need to put the cell in the document somewhere before appending stuff. I'm not sure

naugtur
it seems to work...However appendTo doesnt seem to return a reference to $("input />")
Parhs
it totally should. What does it return then? log it to firebug and take a look
naugtur
A: 

It kind of depends what you want the code to do. If you want to append the input after the last item in cell, then try:

cell=$("<td>");
if(normal.exam_type=="Exam_Boolean")
{
    var input=cell.last().after("<input>");

    input.attr("type","hidden");
    input.attr("name","exam.exam_Normal['" +normal_id_unique + "'].boolean_v");
    input.attr("value",normal.normal_boolean);
};

If this does not do what you desire, maybe you should post some of the html code and a better description of what you want your code to do.

lugte098
thank you im ok
Parhs
you're welcome!
lugte098