Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 753
Tahnoon Pasha Avatar asked May 01 '26 17:05

Tahnoon Pasha


2 Answers

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");
});
like image 88
Gautam3164 Avatar answered May 04 '26 07:05

Gautam3164


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

like image 44
Arun P Johny Avatar answered May 04 '26 07:05

Arun P Johny