Solved

jQuery to Add Image from XML File

Posted on 2014-02-26
5
335 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 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
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 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now