Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery to Add Image from XML File

Posted on 2014-02-26
5
Medium Priority
?
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 2000 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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

705 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