admin 管理员组文章数量: 1086019
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();
本文标签: javascriptget html from a variable by idStack Overflow
版权声明:本文标题:javascript - get html from a variable by id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744006928a2517458.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论