AHmedRef
AHmedRef

Reputation: 2611

Get html content by id selector from external file using JQuery

I want to know if there any possibility to get an element (HTML content) from an external HTML file using the ID selector of this element

The external file (template.html):

<div id="id1">Content 1 </div>
<div id="id2">Content 2 </div>

Is there any process like this :

the current file (index.html)

<script>
var url = "template.html";
var externalHtmlContent = $ajax(url).document.getElementById("id").innerHTML;
</script>

so externalHtmlContent would contain "Content 1" value?

Upvotes: 4

Views: 7475

Answers (2)

v7d8dpo4
v7d8dpo4

Reputation: 1399

$.get(url, function (data) {
    var html = $(data);
    var content_1 = $('#id1', html).text();
    // content_1 is "Content 1"
});

Upvotes: 4

clab
clab

Reputation: 76

Try this:

HTML

<div id="#hiddenDiv" style="display: none"></div>

JS

$("#hiddenDiv").load(url,
   function() { 
      var content = $('#id1').html();
      //...
   }
);

Upvotes: 2

Related Questions