[Resolved] Passing a custom field to the JS in a template
This thread is resolved. Here is a description of the problem and solution.
Problem:
The issue here is that the user wanted to pass a custom field value to their JS code.
Solution:
In order to do this you will need to scrape the value from the page display itself using Javascript. You are not able to add the shortcode directly to the js code.
I'm using Contact Form 7 and have embedded a form in a Content Template. The Content Template is for a CPT called Articles. The Articles have a custom field for URL. Upon submission of the form I'm trying to redirect to that url.
This is the code I'm using in the JS panel:
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = '[types field='link-to-article' output='raw'][/types]';
}, false );
Thanks Shane, I don't really know Javascript but I managed to get the URL to appear in an alert box, so I can grab it from the page. Now I guess I need to pass it to the code I had, but this doesn't work... am I close? This
var myLink = document.getElementById("myLink");
var myURL = myLink.href;
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = (myURL);
}, false );
This causes a redirect but just append myURL to the existing url.