How do I get rid of an a href if an xml node is empty and write another code?
I'm trying to get rid of:
- '<a href="' + link + '"' + 'rel="prettyPhoto"' + 'title="' + linkdes + '"' + '>' '</a>'
if xml node link is empty and write:
- '<img src="' + image + '"' + 'title="#htmlcaption' + ind +'"/>'
instead.
This is the code:
- $(document).ready(function()
- {
- // get xmlfile.xml and define function.
- $.get('/design/designsystems/unitedway/unitedway.xml', function(d){
-
- //Find the child elements of the subchild elements that you wish to display in slider and index the current items of them.
- //('ind' is the index, and 'item' is the current item).
- //Example: $(d).find('child elements of xml here').each(function(ind,item){
- $(d).find('pic').each(function(ind,item){
-
- // Define the variables used from the subchild elements in the xml file.
- // Example: var yourMadeUpName = $(this).find('subchild element from xml here').text();
- var title = $(this).find('title').text();
- var image = $(this).find('image').text();
- var caption = $(this).find('caption').text();
- var thumbnail = $(this).find('thumbnail').text();
- var link = $(this).find('link').text();
- var linkdes = $(this).find('linkdes').text();
-
- //Variable that creates the images with links.
- var html = '<a href="' + link + '"' + 'rel="prettyPhoto"' + 'title="' + linkdes + '"' + '>' + '<img src="' + image + '"' + 'title="#htmlcaption' + ind +'"/>' + '</a>';
- //Variable that creates the html captions.
- var caption = '<div id="htmlcaption'+ind+'" class="nivo-html-caption">' + title + '<br><span class="caption">' + caption + '</span></div>';
-
- //Positions the images and links within the div with an id of #slider.
- $('#slider').append($(html));
-
- //Positions the html captions below it.
- $('#slider').after($(caption));
-
- });
- });
- });
I could just remove the a href if it was empty, but it removes the equal sign after it if the xml node is empty when the code is written out on the page, so it's like:
- <a href rel="prettyPhoto" title class="nivo-imageLink">
Thanks!