0

I want to populate an input field with a parameter from the URL. The parameter isn't set on page load. It's set by jQuery after clicking a link on the site.

The jQuery code:

$('[data-target]').click(function() {
  window.history.pushState(
    null,
    null,
    '?data-target=' + $(this).data('target').replace('#', '')
  );
});

(from here https://stackoverflow.com/a/54540461/1788961)

Is there any way to use parameter with GET after the URL has changed?


For reference: I try to populate an input field from Contact Form 7 (WordPress plugin) and I'm using the "Contact Form 7 Dynamic Text Extension" for that (https://de.wordpress.org/plugins/contact-form-7-dynamic-text-extension/).

The data I want to use comes from Advanced Custom Fields and is stored in a sub field. Therefore I can't use a meta field from the post itself. (I think).

Cray
  • 5,307
  • 11
  • 70
  • 166
  • Can't you just add `$('#yourInput').val($(this).data('target').replace('#', ''))` to the click handler? Why do you need to retrieve it from the URL? – Rory McCrossan Feb 07 '19 at 09:11
  • I need the parameter in the URL because it's the only way I can use it in the form. I'm using the same form for multiple dates and I want to add the date in the input of the form. – Cray Feb 07 '19 at 09:13
  • Right, but my point is that you have access to the value *before* you add it to the URL in the click handler, so why not just work with it there? Are you saying that you need to retrieve this URL parameter on load of the page as well? If so, it's confusing given your statement of 'The parameter isn't set on page load' – Rory McCrossan Feb 07 '19 at 09:14
  • Hmm, I'm not sure I can follow :-) I have access to the value before I add it to the URL. But I'm not aware of the number of dates. And the value is always different. So I need a dynamic version, correct? You can see the HTML in the answer link above if that helps. And no, I don't need the paramter on page load. – Cray Feb 07 '19 at 09:18

0 Answers0