Tahnoon Pasha
Tahnoon Pasha

Reputation: 6018

Using $(tag).load() successfully in jQuery

Hi I'm trying to use jQuery to load an html document into an existing html document.

I've tried using the code below, but the text doesn't load.

I'm not sure why. Could someone point me towards what I'm doing wrong please?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">$("#test").load("test.txt")</script>
</head>

<body>
<div id="test"></div>
</body>
</html>

Upvotes: 3

Views: 91

Answers (2)

GautamD31
GautamD31

Reputation: 28763

Try on DOM ready like

<script type="text/javascript">
   $(document).ready(function(){
       $("#test").load("test.txt");
   });
</script>

And you also forgotted ending ;.You can also try like

$(function(){
     $("#test").load("test.txt");
});

Upvotes: 3

Arun P Johny
Arun P Johny

Reputation: 388316

You need to add it in dom ready

jQuery(function($){
    $("#test").load("test.txt")
})

The problem was when your script is executed the element with id test was not yes added to the dom so the selector $("#test") would return zero elements

Upvotes: 3

Related Questions