You would need to hold onto the original response to do this...
$("#hiddendiv").html(data);
$("#div").text(data);
Then you could get it back out...
var html = $("#hiddendiv").html();
Update based on comment...
You can remove that element before you display it...
var html = $(data);
$('#cookiediv', html).hide();
$('#div').html(html);
Where the cookie message has id="cookiediv" - you may need to adjust the selector to get to this div, but it is almost certainly possible to grab it.
You would need to hold onto the original response to do this...
$("#hiddendiv").html(data);
$("#div").text(data);
Then you could get it back out...
var html = $("#hiddendiv").html();
Update based on comment...
You can remove that element before you display it...
var html = $(data);
$('#cookiediv', html).hide();
$('#div').html(html);
Where the cookie message has id="cookiediv" - you may need to adjust the selector to get to this div, but it is almost certainly possible to grab it.
$.fn.toHtml=function(){
return $(this).html($(this).text())
}
then
$('#div').toHtml()
http://jsfiddle.net/abdennour/fcn9d/
You can do this:
var $html = $('<iframe width="854" height="480" src="http://www.youtube.com/embed/gYKqrjq5IjU?feature=oembed" frameborder="0" allowfullscreen></iframe>');
var str = $html.prop('outerHTML');
console.log(str);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
FIDDLE DEMO
What you want is the outer HTML, not the inner HTML :
$('<some element/>')[0].outerHTML;
Update:
From jQuery 1.8, we can use $.parseHTML, which will parse the HTML string to an array of DOM nodes. eg:
var dom_nodes =
.parseHTML('<div><input type="text" value="val" /></div>'));
alert( dom_nodes.find('input').val() );
DEMO
var string = '<div><input type="text" value="val" /></div>';
$('<div/>').html(string).contents();
DEMO
What's happening in this code:
$('<div/>')is a fake<div>that does not exist in the DOM$('<div/>').html(string)appendsstringwithin that fake<div>as children.contents()retrieves the children of that fake<div>as a jQuery object
If you want to make .find() work then try this:
var string = '<div><input type="text" value="val" /></div>',
object = $('<div/>').html(string).contents();
alert( object.find('input').val() );
DEMO
As of jQuery 1.8 you can just use parseHtml to create your jQuery object:
var myString = "<div>Some stuff<div>Some more stuff<span id='theAnswer'>The stuff I am looking for</span></div></div>";
var $jQueryObject =
.parseHTML(myString));
I've created a JSFidle that demonstrates this: http://jsfiddle.net/MCSyr/2/
It parses the arbitrary HTML string into a jQuery object, and uses find to display the result in a div.
$.parseHTML() parses a string into an array of DOM nodes.
To create the object you need to add $():
.parseHTML(yourString))
Take a look at this fiddle: http://jsfiddle.net/j05ucnfv/
Reference: http://api.jquery.com/jQuery.parseHTML/
Use
$.parseHTML()
instead of
$(HTMLSTRING)
See below JS Code..
var HTMLSTRING = 'Pay using your <img src="visa-src" /> or your <img src="mc-src" />';
var HTMLSTRING1 = '<img src="visa-src" /> or your <img src="mc-src" />';
console.log($.parseHTML(HTMLSTRING));
console.log($.parseHTML(HTMLSTRING1));
Demo Fiddle
I assume you're asking for the full HTML string. If that's the case, something like this will do the trick:
$('<div>').append($('#item-of-interest').clone()).html();
This is explained in more depth here, but essentially you make a new node to wrap the item of interest, do the manipulations, remove it, and grab the HTML.
If you're just after a string representation, then go with new String(obj).
Update
I wrote the original answer in 2009. As of 2014, most major browsers now support outerHTML as a native property (see, for example, Firefox and Internet Explorer), so you can do:
$('#item-of-interest').prop('outerHTML');
With jQuery 1.6, this seems to be a more elegant solution:
$('#element-of-interest').prop('outerHTML');
I think both answers by Matt Schmiermund and Alex Gidan are directing you to the right place.
You should try something like this :
var a = "<div>Your HTML & More</div>";
var span = document.createElement("span");
$(span).text(a)
$(span).html()
this will return the following :
"<div>Your HTML & More</div>"
If you have to do it with JQuery (directly from your JS) you can use .text() / .html() functions:
$('<div/>').text(value).html();
$('<div/>').html(value).text();
If you want to use jQuery, you can use the text(string) method.
$(".selector").text("x < 5");
http://api.jquery.com/text/
Or, Take it simple and do this
var str1 = "x < 3";
str1.replace(/</g, '<');
Here is a function from another question
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
Or, Excellent cheat using jQuery Source
function htmlEncode(value){
return $('<div/>').text(value).html();
}
function htmlDecode(value){
return $('<div/>').html(value).text();
}
Take simple nested example.
var dom_string = '<div>xxx<div>yyy</div></div>';
create HTML DOM elements using $() function of jquery and append wherever you want. i have taken 'body' but you can append anywhere.
$(dom_string).appendTo('body');
Alternatively you can implement this with pure javascript:
var dom_target = document.getElementById("target");
dom_target.innerHTML = dom_string;
Create a dummy element and set its innerHTML to your HTML string.
split the initial string on the comma, then map over the resulting array. On each iteration of map you can then split on the colon, and return a HTML string. You just need to make sure you join the array that map returns into a new string before you add it to the DOM.
const str = 'i1:1,i3:5,i2:5';
const result = str.split(',').map(el => {
const [key, value] = el.split(':');
return `<li id="${key}" value="${value}">${value}</li>`;
}).join('');
const ol = document.querySelector('ol');
ol.insertAdjacentHTML('beforeend', `<ul>${result}</ul>`);
<ol />
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Additional documentation
Destructuring assignment
Template literals
insertAdjacentHTML
I believe that my simple solution using method forEach() and method split() will not be superfluous, which will split the string into an id and a value taken from an array of similar data.
let ul = document.createElement("ul");
document.body.appendChild(ul);
["i1:1", "i3:5", "i2:5"].forEach(function (li) {
let id_value = li.split(":");
ul.innerHTML += "<li id=" + id_value[0] + ">" + id_value[1] + "</li>";
});
console.log(ul.outerHTML);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
$('.post .posttext a').html(YourfunctionThatReturnsAString());
This should do it
$('.post .posttext a') -> this returns an array of a elements.
$('.post').eq(the post number).find('.posttext a') -> replace "the post number" with the number of your post if you want to target something more specific.
You can then call the text("your text") method to set the text.