Solved

jQuery to Add Image from XML File

Posted on 2014-02-26
5
340 Views
Last Modified: 2014-03-29
I've just about got this working (with the help of others here).  I'm new at using jQuery so please be patient.  Here's what I'm trying to do.

I have the URL for three images within <image> tags in my XML file (attached).  I now want to use jQuery to insert these images into my HTML page.  I know I need to set both the 'class' and the 'src' attributes in here somewhere but I can't quite manage to make it work.  I know the jQuery in my .js file (attached) is wrong however it works fine until it gets to inserting the images. Any help?  Thanks!
pbkpluscom-jquerytest.html
pbkpluscom-jquerytest.xml
pbkpluscom-jquerytest.css
pbkpluscom-jquerytest.js
0
Comment
Question by:penlandt
  • 3
5 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 39891277
you're Ajax call needs to be inside the $(function() ...) otherwise the elements may not exist yet.

$(function() {
	$('h1').addClass('standout');


$.ajax({
	type: "GET",
	url: "http://www.pbkplus.com/home/pbkpluscom_jquerytest.xml",
	dataType: "xml",
	success: function(xml) {
		$(xml).find('story').each(function(){
			$('<h2>').html( $('headline', this).html() ).appendTo('content');
			$('<p>').html( $('content', this).html() ).appendTo('content');
			$('<img class='imgfloatr'>').html( $('image', this).html() ).appendTo('content');
			});
	}
});
});

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 39891291
more specifically this line, which has another issue you should be aware of. You should use double quotes to encapsulate the entire string and single quotes for strings within a string. if you only use the one type you'll run into problems as the system won't knew where a string starts and ends.

$('<img class='imgfloatr'>').html( $('image', this).html() ).appendTo('content');

needs to be

$("<img class='imgfloatr' src='+$('image', this).html() +'>").appendTo('content');
0
 
LVL 43

Expert Comment

by:Rob
ID: 39891297
the .appendTo should reference an element but you've specified "content", which I can only find in the xml. this should be an element in your html, not the xml, such as "body"out another element such as a div with an ID. e.g. .appendTo("div#idhere")
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why "Mobile First"? 5 19
On Load function does not trigger on iPhone 4 37
JQuery Detecting page post back 6 12
parsing xml using powershell 6 18
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question