i have a variable name value in my javascript code that contain html data.i want to get the data inside form specific id that is inside #myDiv
var value="<div >
<p class="">Hi [email protected]</p>
<br/>
<br/>
<br/>
<div id="myDiv">
sgdhsagdhagh
dhsajdgasj
cjzjcg
</div>
</div>"
Thanks
i have a variable name value in my javascript code that contain html data.i want to get the data inside form specific id that is inside #myDiv
var value="<div >
<p class="">Hi [email protected]</p>
<br/>
<br/>
<br/>
<div id="myDiv">
sgdhsagdhagh
dhsajdgasj
cjzjcg
</div>
</div>"
Thanks
Share Improve this question asked Aug 1, 2016 at 12:26 pankaj malikpankaj malik 1071 gold badge2 silver badges15 bronze badges 3-
$(value).filter('#myDiv').html()
– Tushar Commented Aug 1, 2016 at 12:27 - it will work like this $(value).find('#myDiv').html() thanks anyway – pankaj malik Commented Aug 1, 2016 at 12:36
- Consider accepting the answer which helped you most, thats the best way to say thanks see meta.stackexchange./questions/5234/… – Satpal Commented Aug 1, 2016 at 12:49
5 Answers
Reset to default 4You should create a DOM element, then traverse up to it and get html()
or .text()
var value = '<div >\
<p class="">Hi [email protected]</p>\
<br/>\
<br/>\
<div id="myDiv">\
sgdhsagdhagh\
dhsajdgasj\
cjzjcg\
</div>\
</div>';
alert($(value).find('#myDiv').html())
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
If you need to get parent element, then use
alert($('<div />', {html: value}).find('#myDiv').html())
How about you do like this?
var variable = "stuff n things";
document.getElementById( 'myDiv' ).innerHTML = variable;
Thats the native way :)
EDIT:
if you want to get the data you just do like this:
var variable = document.getElementById( 'myDiv' ).innerHTML;
That will give you the markup of whats inside the div.
Or use document.querySelector("#myDiv")
to get the first match on the selector.querySelectorAll
returns array.
You can use the HTML of the value
variable to create an jQuery
object. From this object you can search for the element with the id
, and get its HTML content:
var html = $(value).find('#myDiv').html();
console.log(html); //sgdhsagdhaghdhsajdgasjcjzjcg
Or a pure javascript implementation:
var el = document.createElement('div');
el.innerHTML = value;
var html = el.getElementById('myDiv').innerHTML;
console.log(html); //sgdhsagdhaghdhsajdgasjcjzjcg
Using plain Javascript
use:
document.getElementById("myDiv").innerHTML;
Using jQuery
:
$("#myDiv").html();
There are Two ways- Angular way-
var elem = angular.element(value);
var innerDive=elem[0].getElementById('myDiv');
var text= angular.element(innerDive[0]).innerText;
Jquery-
var text= $(value).find('#myDiv').text();