0

If I have a jQuery obj as;

var input = $('<input />', 
              {'type': 'text','name':currElmModelAttr, 'style':'width:100px' ,
               'data-model-id': currElmModelId, 'data-model-attr': currElmModelAttr,
               'class':'datePicker', 'value': $(this).html()}
             );

Can I use any method to convert it into the form

<input type="text".... />

So that I can directly say

$(this).html(aboveHTMLCode);
Danyu
  • 509
  • 3
  • 7
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • Yes, but why? You can use `.append()` instead of `.html()` to insert it, or if you need a copy, use `input.clone()`, like `$(this).empty().append(input.clone())` – cookie monster Jun 28 '14 at 13:49
  • actually i want to replace the existing html...kind of inline edit...div > input > div – copenndthagen Jun 28 '14 at 13:50
  • cookie's comment + `input[0].outerHTML` – Rajaprabhu Aravindasamy Jun 28 '14 at 13:50
  • There is no HTML in the DOM. If you want to replace existing elements, then the `.empty()` method I added to my comment will do that. It's more efficient than serializing the element to HTML, and then parsing it back into a new DOM element. – cookie monster Jun 28 '14 at 13:51
  • http://stackoverflow.com/questions/19812510/converting-html-element-to-string-in-javascript-jquery – Luke Jun 28 '14 at 13:52
  • http://stackoverflow.com/questions/652763/jquery-object-to-string – Luke Jun 28 '14 at 13:52
  • Oh wait, are you saying that you want to remove the `input` element and show its HTML representation instead? Or did you only want to show its `value`? I think we need more detail. – cookie monster Jun 28 '14 at 13:58
  • `$(this).html($('
    ').append(aboveHTMLCode).html());`
    – Jared Farrish Jun 28 '14 at 13:58
  • so initially i have a label like 05/01/1984 ...on click i want it to be replaced with an input box with the same date value in it... – copenndthagen Jun 28 '14 at 13:58
  • Using `$('')` generates a new HTML Element. Its just a matter of adding it, via jquery or native js. So yes use any method to convert it to the form ` – Lex Jun 28 '14 at 13:59
  • @testndtv: Assuming your label is `this`, you can do `$(this).replaceWith(input)` if I'm understanding you correctly – cookie monster Jun 28 '14 at 14:08

0 Answers0