0

In the code var check = $($.parseHTML("<input type=\"checkbox\" />")[0]); I would like to know why:

  • check.html() is a empty string
  • check[0] is <input type="checkbox"></input> AND
  • check[0] + 'test' is "[object HTMLInputElement]test"

And I would to know what do I have to do to get <input type="checkbox"></input>test

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • 1
    Are you looking to just add a checkbox to your existing HTML? – j08691 Feb 25 '15 at 17:21
  • check[0] + 'test' as you concat different object type check[0] is converted into string. – Ôrel Feb 25 '15 at 17:23
  • 1
    Fix your input tag: `` There is no closing tag (it is a *void element*) http://stackoverflow.com/questions/3558119/are-self-closing-tags-valid-in-html5 – cssyphus Feb 25 '15 at 17:24

2 Answers2

3

check will be a jQuery wrapper around an <input type="checkbox" /> element. As such, it contains no other elements, so check.html() returns "". Remember that .html() returns the stuff inside the element, not the element itself.

All jQuery wrappers are list-like; in this case, the list has only one element (that input), which is found at check[0].

check[0] + 'test'

means "add a string to this input element". As-is, that makes no sense, so the string representation of input is used instead ("[object HTMLInputElement]").

If you want the string "test" after the element, you'll need to add that element to the DOM, then append the string after it.

$('#somecontainer').append(check).append("test");
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

Just drop the parseHTML.

$('<input type="checkbox" />')  // Gives you an array of new elements

$('<input type="checkbox" />')[0] // The new input element

From there... don't concatenate HTML. Insert the elements where they are supposed to be. Otherwise you may run into issues with escaping, should you use text in the context of HTML.

Brad
  • 159,648
  • 54
  • 349
  • 530