Jquery: Load Txt File and Insert into Div

jQuery: load txt file and insert into div

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);
}
});
});
});

jQuery: load txt file and insert into div

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);
}
});
});
});

Insert text into a div with jquery load

There is a no direct way to get data from external file in jquery.
But via ajax its possible.

$(document).ready(function() {
$("#loadData").click(function() {
$.ajax({
url : "articlename.txt",
dataType: "text",
success : function (data) {
$("#content").html(data);
}
});
});
});

Jquery - How to read a txt file and append into a form

You can use .load() to get the contents of a file and insert it into the matched element.

var append_data = '<div id="area_'+country_id+'"></div>';
$("#text_boxes").append(append_data); //append new select options in main div

$("#area_"+country_id).load("path/to/file.html"); // load html file

jquery - Read data from txt file and assign to different variables

Simple:

Have a file (doc.json) with contents:

{
"title" : "some title",
"body" : "some body text"
}

And on your page

$.getJSON( "doc.json", function( data ) {
$("#title").text(data.title);
$("#body").text(data.body);
});

Load text from specific div in text file using ajax

Since your text files contains html markup, you can manipulate then using jQuery.

success: function (data) {
var people = $(data),
john = people.filter('#John_Resig');

$(".demo-cb-tweets").empty().append(john);
}

Wrapping a string of html in a jQuery object turns it into a jQuery object that you can then use and insert into the dom, ie: $('<div>Test</div>').addClass('test-class').appendTo('body');

EDIT: Pulling out names:

You can pull out names from your text file in the same manner. For example, on page load if you had an ajax call to the text file that would initialize all the times. The following code would take your text file, and loop over each container element (in your example, John_Resig and Tim_Berners_Lee):

success: function (data) {
var people = $(data);

people.each(function (i, person) {
var name;
if (person.nodeType !== 3) { // Your text file example had a blank line between the containing div elements... which loads as a nodeType of 3, so we'll want to skip those.
name = $('.contact', person).text(); // This would grab the text inside of the div with the class 'contact'.
// do stuff with that name here...

}
});
}


Related Topics



Leave a reply



Submit