Skip to content Skip to sidebar Skip to footer

Getting Innerhtml From A Javascript Variable

I make an ajax call from my page , and then in response I also get some html like this what I want is to get Inner

Solution 1:

you can create a jQuery wrapper for the variable content and then extract the inner html using .html()

vardata = '<parent id="1"><child></child></parent>'var x = $(data).html()

Solution 2:

Pure JS if you like -> http://jsfiddle.net/eztZm/

//getvar get_html = document.getElementById("parent").innerHTML;
console.log(get_html);
//set document.getElementById("parent").innerHTML = "new html";

https://developer.mozilla.org/en-US/docs/Web/API/Element.innerHTML

Solution 3:

html code used:

<parentid="1"><child>candy</child></parent>

first approach:

var parent = document.getElementById("1");
var child_text = parent.firstChild.innerHTML;

to make a long story short:

document.getElementById("1").firstChild.innerHTML

will deliver "candy" (without jQuery) :)

Post a Comment for "Getting Innerhtml From A Javascript Variable"