I'm trying to get the HTML of a selected object with jQuery. I am aware of the .html()
function; the issue is that I need the HTML including the selected object (a table row in this case, where .html()
only returns the cells inside the row).
I've searched around and found a few very ‘hackish’ type methods of cloning an object, adding it to a newly created div, etc, etc, but this seems really dirty. Is there any better way, or does the new version of jQuery (1.4.2) offer any kind of outerHtml
functionality?
Simple solution.
Short and sweet.
or event more sweet with help of arrow functions
or without jQuery at all
or if you don't like this approach, check that
I came across this while looking for an answer to my issue which was that I was trying to remove a table row then add it back in at the bottom of the table (because I was dynamically creating data rows but wanted to show an 'Add New Record' type row at the bottom).
I had the same issue, in that it was returning the innerHtml so was missing the TR tags, which held the ID of that row and meant it was impossible to repeat the procedure.
The answer I found was that the jquery
remove()
function actually returns the element, that it removes, as an object. So, to remove and re-add a row it was as simple as this...If you're not removing the object but want to copy it somewhere else, use the
clone()
function instead.Anothe similar solution with added
remove()
of the temporary DOM object.Extend jQuery:
And use it like this:
$("#myTableRow").outerHTML();
you can also just do it this way
where id is the id of the element that you are looking for