Pulling page title into hidden form field

Hey everyone, I’ve used the solution to pull the page URL into a hidden form field on various sites without problems:

<script type="text/javascript">
            
window.onload=function()
{document.getElementById('pageurl').value = window.location.href;}

</script>

<input type="hidden" id="pageurl"  name="Form-location" value="pageurl" />

But I’m having difficulty working out the identifier for value of the “page-title” to replace window.location.href,

:slight_smile: Please advise.

Hi,
Firstly, make sure you have the “Title Tag” set for the page you wish to run this script on.

If it’s a static page, you can set the title under page settings, for example:

If it’s a Dynamic page (CMS single page), make sure you set the Title for your CMS Single page template using “Add Field” option, for example:

Then, in a similar way you’re trying to fetch and set the location.href value, for title you can use “document.title”, for example add this code to your function:

document.getElementById(‘page-title’).value = document.title;

Overall, you’ll also need to modify your code like this:

<script type="text/javascript">
function getData(){
document.getElementById('pageurl').value = window.location.href;
document.getElementById('page-title').value = document.title;
}
window.onload= getData();

</script>

<input type="hidden" id="pageurl"  name="Form-location" />
<input type="hidden" id="page-title"  name="Form-location-title"  />

Hope this helps.