0

I am using Jquery, making table cells that contain multiple divs. the divs contain some data and a radio button. my code works, but i want to know why i had to use a "middle-man" variable. seems really weird to me, was very frustrating, and im curious why. perhaps i just dont undestand this well enough. here is my code

    // my radio button
    var rad_button = $('<input type="radio"...'
    // my object, div w some attributes, text
    var my_Object = $('<div class="selected...'
    rad_button.prependTo(my_Object);

    var middle_man = my_Object;
    middle_man.appendTo(bigger_object);

this is the only way i could get this to work. i tried the more direct way:

    my_object.appendTo(bigger_object);

but that didnt work. anyone able to tell me why this is done this way?

Musa
  • 96,336
  • 17
  • 118
  • 137
  • 1
    That's a `TYPO` my_Object != my_object – Musa Jul 16 '12 at 22:11
  • @Olaf, there is an empty line between your call to `prependTo()` and your having to alias `middle_man` to `my_Object` for your code to work. Is this meaningful, and does part of the code under the line break reside in some kind of asynchronous callback, or is it just a matter of casing between `my_Object` and `my_object`? – Frédéric Hamidi Jul 16 '12 at 22:22
  • make a not working fiddle with that and it's probably like Musa said. – TecHunter Jul 16 '12 at 23:06

1 Answers1

0

Can't you just chain your append calls together on the table cell you are creating?

$('<td />')
  .append($('<input />', { 'type':'radio' }))
  .append($('<div />', { 'class':'selected' }));

Should output HTML

<td>
  <input type="radio" />
  <div class="selected"></div>
</td>

No middle man.

cmd.prompt
  • 954
  • 7
  • 14