3

I have an input box created by jquery like so:

val input = $('<input class="pick_date" ... />')

but the .html() method on input does not return the string entered inside the $. does anyone know why?

edit: Ah, I understand the problem. Is there a way to get the html representation of the entire input box and not just the entry?

Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94

3 Answers3

5

you are passing <input /> which is a self-closing tag.

If you were passing <input>Html here</input> (which is valid XML but not HTML to my knowledge), you could retrieve the "Html here" part with the .html() function like so:

var input = $('<input class="pick_date">Html here</input>');
alert(input.html());

In addition to your edited question:

$('<input />').outerHtml();

this should work.. :)

with this ofcourse (source):

(function($) {
    $.fn.outerHTML = function() {
        return $('<div>').append( this.eq(0).clone()).html();
    };
})(jQuery)
Ropstah
  • 17,538
  • 24
  • 120
  • 194
0

I think maybe you are looking for append():

$("div#form").append('<input class="pick_date" ... />');
pifantastic
  • 861
  • 6
  • 13
  • Might want to additionally read this: http://stackoverflow.com/questions/268490/jquery-document-createelement-equivalent – pifantastic Sep 08 '09 at 21:59
0

Just had to deal with this problem.

If you are using ASP.NET, it could be because ASP.NET changes the id names, if you add "runat="server".

So instead of doing this:

<td id="mytd" runat="server"></td>
$('#mytd').html()

Try doing this:

<td id="mytd" class="myclass" runat="server"></td>
$('.myclass').html()
live-love
  • 48,840
  • 22
  • 240
  • 204