You are viewing limited content. For full access, please sign in.

Question

Question

Appending to an HTML Field

asked on January 30, 2017 Show version history

I am using an HTML field to display data (with a link) using the .innerHTML method.  The data to be displayed is coming from a collection that will be populated via Lookup.  For 1 result, the code works great, but it is when I have multiple results where I need help.  Right now, it always takes the LAST record in the collection and what I need is for it to APPEND all the values together.  See code and description below:

Existing Code:

$(document).ready(function (loadStops) {
  	$('.Submit').hide();
function loadStops() 
	{

		$('.cf-collection-block ul').each(function(i) 
		{
          $(this).each(function(e)
			{
            var load_id = $('.load input').val() ; //This is the load ID that will be embeded in the URL.  This is NOT in the collection
            var stop = i + 1;//My Stop number = iteration +1
            var stopStr = $(this).find('.street input').val()
            var stopCity = $(this).find('.city input').val()
            var stopState = $(this).find('.state input').val()
            var stopZip = $(this).find('.zip input').val()
            var stopAdd = stopStr + ' ' + stopCity + ', ' + stopState + ' ' + stopZip;
			var target = 'target="_blank"'; //Open link in new tab
			var styleHTML = '<font size="5"> <p style="text-align: center;">'//CSS Styling of the HTML field
			var link = "<a " + target + "href='https://forms.protrans.com/forms/pMKMH/?Load=" + load_id +"'>Stop #: " + stop + " " + load_id + "</a>";
      		document.getElementById("q3").innerHTML = styleHTML + link + "<br>" + stopAdd + "</p></font>";
            });
		});

      	}
	$('.change').change(loadStops);
 });

Here is the current result:

Note: When I add a second record in the collection, it automatically takes the LAST record and over-writes the existing record.  

Desired Result:

Thanks,

Nate

0 0

Answer

SELECTED ANSWER
replied on January 30, 2017

Hi Nate,

I'm not entirely sure from first glance which needs to be updated, but I think the issue might be that for each iteration of the "each" loop, you are setting the innerHTML of element #q3 instead of adding to it. If you change line 20 above to use

document.getElementById("q3").innerHTML += ...

instead of

document.getElementById("q3").innerHTML = ...

does it get you the intended behavior?

Cheers,

1 0
replied on January 30, 2017

That was it!  I was hoping it was a simple fix!  Thanks James!  

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.