Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: load txt file and insert into div

I want to load a *.txt file and insert the content into a div. Here my code:

js:

$(document).ready(function() {     $("#lesen").click(function() {         $.ajax({             url : "helloworld.txt",             success : function (data) {                 $(".text").html(data);             }         });     }); });  

html:

<div class="button">     <input type="button" id="lesen" value="Lesen!" /> </div>  <div class="text">     Lorem Ipsum <br /> </div> 

txt:

im done 

If i click on the button firebug report following error:

Syntax-Error im done 

I don´t know what to do :-(

like image 272
Khazl Avatar asked Jun 24 '11 16:06

Khazl


2 Answers

You need to add a dataType - http://api.jquery.com/jQuery.ajax/

$(document).ready(function() {     $("#lesen").click(function() {         $.ajax({             url : "helloworld.txt",             dataType: "text",             success : function (data) {                 $(".text").html(data);             }         });     }); });  
like image 98
Dogbert Avatar answered Oct 08 '22 21:10

Dogbert


You could use jQuery.load(): http://api.jquery.com/load/

Like this:

$(".text").load("helloworld.txt"); 
like image 45
jncraton Avatar answered Oct 08 '22 21:10

jncraton