How to get content of element by id from HTML object using JavaScript (JQuery)

I will write the following code to access the "jQueryPage.aspx" page and receive data from it using jQuery

<script type="text/javascript">

    $.get
    (
        "JQueryPage.aspx",
        function(data) {
            alert("Data Loaded: " + data);
          } 
    );

</script>

      

"JQueryPage.aspx" is just a page containing a DIV called "resultsDIV" which contains the data I want to return the

above code return data variable containing the html "JQueryPage.aspx" and I want to get the content of the DIV from it ..
I have 2 questions:
1- how can I extract the content of the DIV from the data object
2- is this the best way to get this data?

+2


a source to share


3 answers


Try using something like this:

<script type="text/javascript"> 

    $.get 
    ( 
        "JQueryPage.aspx", function(html) { 
            var page = $(html);
            var div = $('#div1', page);
        }  
    ); 

</script> 

      



you can also look at $.load

function

+2


a source


Jsut wraps data in a jquery call and you can use it as usual:



$.get
    (
        "JQueryPage.aspx",
        function(data) {
            var dataDom = $(data);
            $(someSelector, dataDom).each(function(){
                alert($(this).html());
            });
          } 
    );

      

0


a source


  • If the html markup is JQueryPage.aspx

    valid xml, you can use the dom parser to get the div you want.
  • It depends if you want to add the resulting html to the existing DOM with a call document.appendChild

    , yes. But if you need to parse and read values ​​from the extracted data, no, it is not. Pass the data as JSON or xml string.
0


a source