1

I would like to ask if anybody sees what is going on. I have plenty experience with jQuery append elements while creating them, I made this several times but now Im struggling since 2 hours without being able to find my mistake, perhaps you can help me.

I'm trying to append the following as stated below:

var contactoHolder = $('<div />', {'class':"contactoUnexpanded",'id':indexed});
contactoHolder.append(infoArray[0]);
$('<div />', {'class':"editDelete", 'id':indexed+'editDelete'}).appendTo(contactoHolder);     
$('<span />', {'class':"edit",'id':indexed+'_edit'}).appendTo('#'+indexed+'editDelete');
$('#'+indexed+'_edit').html('edit');
$('<span />', {'class':"delete",'id':indexed+'_delete'}).appendTo('#' + indexed + 'editDelete');
$('#' + indexed + 'delete').html('delete');
$('<span />', {'class':"contenidoDelContacto", 'id':indexed + 'Content'}).appendTo(contactoHolder);
$('#' + indexed + 'Content').html('<br />' + infoArray[1] + '<br />' + infoArray[2] + '<br />' + infoArray[3] + '<br />' + infoArray[4]);
$('#content').append(contactoHolder); 

The only elementswhen I render that get definitively in the DOM are

<div id="19" class="contactoUnexpanded contactoExpanded">
  Santiago
  <div id="19editDelete" class="editDelete"></div>
  <span id="19Content" class="contenidoDelContacto"></span>
</div>

I don't know why is not appending the rest of the elements as it should.

Thank you for your help.

3
  • Mind posting a fiddle? What does infoArray contain? Are you getting any console errors? Commented Nov 26, 2013 at 15:56
  • no console error. infoArray is ["Santiago", "av del parque", "099625635", "6826910", "[email protected]"] as example Commented Nov 26, 2013 at 15:57
  • I expect that the word 'edit' gets into the $('#'+indexed+'_edit') element created above Commented Nov 26, 2013 at 15:58

1 Answer 1

1

Your problems start here:

$('<span />', {'class':"delete",'id':indexed+'_delete'}).appendTo('#' + indexed + 'editDelete');

Since you haven't appended any of your elements to the DOM yet, ('#' + indexed + 'editDelete') doesn't match anything. The selector engine only looks at elements currently in the DOM, not those in JavaScript variables.

You can solve this by appending the contactoHolder right away: http://jsfiddle.net/mblase75/ptpgJ/

var contactoHolder = $('<div />', {'class':"contactoUnexpanded",'id':indexed});
$('#content').append(contactoHolder); 
// now the rest of your code

Alternatively, try assigning each new jQuery element a variable name, and reference those variables instead when appending or modifying them. This should be more efficient, but the difference will probably be imperceptible to your users unless you're doing this hundreds or thousands of times in a row:

var div1 = $('<div />', {'class':"editDelete", 'id':indexed+'editDelete'}).appendTo(contactoHolder);     
// ...
$('<span />', {'class':"delete",'id':indexed+'_delete'}).appendTo(div1);

All together: http://jsfiddle.net/mblase75/B33wW/

var contactoHolder = $('<div />', {
    'class': "contactoUnexpanded",
    'id': indexed
});
contactoHolder.append(infoArray[0]);
var div1 = $('<div />', {
    'class': "editDelete",
    'id': indexed + 'editDelete'
}).appendTo(contactoHolder);

var span1 = $('<span />', {
    'class': "edit",
    'id': indexed + '_edit'
}).appendTo(div1);
$(span1).html('edit');

var span2 = $('<span />', {
    'class': "delete",
    'id': indexed + '_delete'
}).appendTo(div1);
$(span2).html('delete');

var span3 = $('<span />', {
    'class': "contenidoDelContacto",
    'id': indexed + 'Content'
}).appendTo(contactoHolder);
$(span3).html('<br />' + infoArray[1] + '<br />' + infoArray[2] + '<br />' + infoArray[3] + '<br />' + infoArray[4]);
$('#content').append(contactoHolder);
Sign up to request clarification or add additional context in comments.

1 Comment

amazing answer, what I needed to know and explained clearly, didnt realize about as I used to append each one at the time and now Im doing it only once kinda backbone el. thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.