0

I'm trying to convert the pure html of a Node or HTMLElement to string but i can't seem to get the right result as shown in the console. That means just the html element, not it's child elements...

CODE

function eventData(e){
    return {
        'sender': e.srcElement || e.target,
        'event': e || window.event,
    };
};

$(document).ready(function(e){
    var $elm = $("<input class='test' data-type='text only'/>").attr({'type':'button','value':'Test'});
    $elm.appendTo('#target');
    $elm.click(function(e) { alert(eventData(e).sender.value); });
    $('#asHTML').text($elm[0].toString());

    //Console is the best way to display a node object;
    //Open your console to see the result;
    console.log($elm[0]);
});

I'm just wondering if any one of you has successfully done this!

DEMO: http://jsfiddle.net/0xfwLfwj/1/

EDIT Updated fiddle with better explanation: http://jsfiddle.net/ttoom9hc/10/

Jeroen Vorsselman
  • 803
  • 1
  • 9
  • 19

2 Answers2

0

If this is what you want to display -

<input class="test" data-type="text only" type="button" value="Test">

Then this is the piece of code for it -

$('#asHTML').text($elm[0].outerHTML);

Here is a fiddle

Ramiz Wachtler
  • 5,623
  • 2
  • 28
  • 33
0

have you tried

$elm.html()

That should give you the HTML of that element as a string. http://api.jquery.com/html/

jcruz
  • 718
  • 6
  • 13
  • Doing this works, but it also reads it's child elements. I only need the actual HTML element. Maybe there's a way to avoid the child elements? – Jeroen Vorsselman Sep 15 '14 at 00:04