Search
Other Sourcerer questions
Forum

JavaScript through Sourcerer doesn't execute

Patrick Gay's Avatar Patrick Gay
I'm using Joomla 3.10.1 and Sourcerer Pro 9.0.1

I'm trying to setup a button in javascript that will cause a getJSON to return some data and then parse it out and create a new URL using the JSON data that was returned and ultimately do an open.window on the new url. But for now I'm just trying to get the url to display on the screen.

This works like a charm in JSFiddle.

When I try to do it inside a Joomla article using Sourcerer, all the code is there, but nothing is showing up when I click the button. I can't tell if Sourcer isn't handling it or if there is something wrong with the way Joomla and Javascript and Sourcerer are working together against me, (apparently), or if I have a problem in my JavaScript.

Any thoughts are much appreciated.
{source}
[[h2]]Sign Documents Now[[/h2]]

[[p id="getdocusignurl"]][[/p]] //this is where the URL should appear

[[button]]Sign Documents Now[[/button]]

[[script type="text/javascript"]]

jQuery(document).ready(function(){
  jQuery("button").click(function(){
jQuery.getJSON('https://us12b.sheltermanager.com/service?account=xxxxx&method=json_report&username=xxxxx&password=xxxxx&title=DocuSign+Integration+for+Volunteers&email=[[?php echo $user->email; ?]]',
		function(data) {
			var dt = JSON.stringify(data).replace('[', '').replace(']', '');
			if (dt) {
				var result = JSON.parse(dt);
				var firstname = result.OWNERFORENAMES;
				var lastname = result.OWNERSURNAME;
				var address = result.OWNERADDRESS;
				var city = result.OWNERTOWN;
				var state = result.OWNERCOUNTY;
				var email = result.EMAILADDRESS;
				var zip = result.OWNERPOSTCODE;
				var uri = "https://na3.docusign.net/Member/PowerFormSigning.aspx?PowerFormId=a221aedd-bce2-44fd-bb2d-17d513486bd8&env=na3&acct=xxxx&v=2&Volunteer_UserName=" + firstname + " " + lastname + "&Volunteer_Email=" + email + "&Volunteer_Address=" + address + "&Volunteer_City=" + city + "&Volunteer_State=" + state + "&Volunteer_Zip=" + zip;
				var url = encodeURI(uri);
				console.log(url)
			  	document.getElementById("getdocusignurl").innerHTML = url;
          }
          })
          })
          });
[[/script]]
{/source}

Here is what I see in the Dev Console:
Peter van Westen's Avatar Peter van Westen ADMIN
Sourcerer is only responsible for placing the code you tell it to. It is not responsible for whether that code works or not.

Check the HTML output of your page (so View Source, not the Inspector) and see if the scripts are placed as you want it.
Also check the browser console to see if you are getting any javascript errors. If so, that should help you to see what is wrong with the code.
Please post a rating at the Joomla! Extensions Directory
Peter van Westen's Avatar Peter van Westen ADMIN
By the way, your javascript code will add the click function on the first button element it finds. It is not very specific. So the issue might be you have multiple button elements on your page.
I suggest you give the button an id, and use that to attach the click event.
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription