0

I usually use "getElementById()" to detect internal file data . I tried this to detect data from an external file but its not work :

<script>
var outterPage ="dom1.html";
var temp=outterPage.getElementById("abc").innerHTML;
</script>

what can i do to detect data from external file ?

4
  • 1
    what do you mean by "internal" and "external" ? getElementById returns you a DOM element by id. Commented Dec 22, 2015 at 21:07
  • 1
    Is the page dom1.html on the same domain? Commented Dec 22, 2015 at 21:09
  • @Jack Zelig same domain and the same folder Alp i mean : internal : same file . external : another file . Commented Dec 22, 2015 at 21:39
  • 1
    api.jquery.com/load/#loading-page-fragments is this what your're looking for? Are you trying to update your page with the data from the other page? Commented Dec 22, 2015 at 21:42

2 Answers 2

2

You can use the jQuery function $.get to read the contents of a URL into a variable. Then you can parse that into a DOM element to find parts of it.

$.get("dom1.html", function(data) {
    var temp = $("<div>", { html: data }).find("#abc").html();
    // do something with temp
});

You can also specify the ID in the URL argument to $.get, so jQuery will just return the contents of that part of the page.

$.get("dom1.html #abc", function(temp) {
    // do something with temp
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use jquery .load() to load the external file into a dom element and then get its value.

<script>
$(document).ready(function(){
$("#holder").load("dom1.html");
var temp=$("#holder").find("#abc").innerHTML;
});
</script>

5 Comments

$() : what is this mean ?
$() is short hand for $(document). Changed it in the code for avoiding confusion.
@anas That's basic jQuery syntax. You used the jquery tag, don't you know jquery?
@Barmar: It's my fault. I provided a jquery solution without knowing his competence with the language.
@DinoMyte No, it seems like it's his fault for tagging the question with jquery when he has no idea how to use it.

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.