最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get html from a variable by id - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

5 Answers 5

Reset to default 4

You 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 Javascriptuse:

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();
发布评论

评论列表(0)

  1. 暂无评论