Search code examples
jqueryappendto

jquery - syntax for including a jquery object as a text or html property


I am not sure what is the correct way of writing the code, or is it even a possible jquery format.

I have

for(i=0; i<10; i++) {
  var inner = $('<div/>', {text: 'it works'});
  var x = ' xyz ';

  var row = $('<div/>', {
    id: 'foo',
    'class': 'abc',
    html: 'test'+ x + inner.text(),
    css: {'padding-left': '5px'}
  });

  row.appendTo('body');
}

The output is test xyz [object Object]

What is the correct syntax for displaying the inner variable so that the correct output would be 'test xyz it works' ?

I know that it would work with using inner.appendTo(row)

A secondary question: If I could get the code to work, what would be faster? appendTo or defining the html with a variable.

TIA


Solution

  • You'll want html: 'test' + x + inner.html(); or html: 'test' + x + inner.text();