0

How to get innerHTML of an element from another web page using JavaScript?

Example:

<script>
  $.get(url, function(response) {
     var $response = $(response); // Code not working from this line
     alert($response.find('.result').html()); // get innhtml of element by class name from response
  });
</script>
1
  • alert your response see what you are getting Commented Jun 26, 2013 at 7:38

4 Answers 4

1

That depend if your response contain html than this will work

    <script>
      $.get(url, function(response) {
         $('yourelement_selector',response).html();
      }
    </script>
Sign up to request clarification or add additional context in comments.

Comments

1

Problem 1: <scrip> needs to be <script>

Problem 2: You haven't finished your function call to get, add ); to the end of your script

Aside from that, given suitable input, the script works as desired.

However, the vagaries of how jQuery (and/or the underlying browser methods it calls) mean that if the elements you are trying to match are children of the body element in the document you are loading, then they will be top-level elements in the jQuery object so you won't find them with find (as they are not descendants). If you want to match elements like that, use filter instead.

1 Comment

@JAslu — That site is not giving JavaScript on my site permission to read content from it.
0

JQuery's .load() will do exactly that:

$('#result').load('ajax/test.html #container');

More info here: http://api.jquery.com/load/

Comments

-1

You can use below example of post method to get data from other site and put that data into your site div/span etc. data container.

$.post("http://www.otherweburl.com",function (data) {
        document.getElementById("htmlContainerDivOrSpanETC").innerHTML=data;
});

1 Comment

The question is asking how to get an element from another page, not the HTML of the entire page. It says nothing about using post, and nothing about the site being external.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.