0

I am trying to pull a variable from a url and populate a hidden variable in my form using jquery.

I've been stuck on this for a few hours now, and I'm hoping someone better at jquery than me can help me out. I've found several solutions, but none of them seem to work for my code. I'm able to use an alert and confirm that I am pulling the 'ad' value from the string and assign to my 'adtest' variable, but nothing I try ever updates the 'meta_adtracking' hidden variable in my code. Any input is greatly appreciated!

My url is: http://www.mysite.com?ad=cl55

The script I am using to pull the variable from the query string:

<script type="text/javascript">
function getURLParameter(name) {
return decodeURI(
    (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

jQuery(document).ready(function(){
    var adtest = getURLParameter('ad');
    $('input[name=meta_adtracking]').val(adtest);
});
</script>

My form code is:

<form accept-charset="utf-8" action="https://app.getresponse.com/add_contact_webform.html" method="GET">
...
    <input type="hidden" id="adtrack1" name="meta_adtracking" value="" />
...
</form>

Thanks in advance!

jhohman
  • 37
  • 1
  • 9
  • Any reason for not just doing `$('#adtrack1').val(window.location.search.split('=')[1]);` – adeneo Apr 22 '13 at 20:51
  • There's no form on that page. – Barmar Apr 22 '13 at 20:52
  • @Barmar - I'm guessing mysite.com is'nt really his page ? – adeneo Apr 22 '13 at 20:52
  • @adeneo yeah, didn't notice the phony domain name until after I posted.... Why would someone tell us the URL if it's not really the URL? – Barmar Apr 22 '13 at 20:53
  • the live url is: http://www.grooveapp.com/test/index2.html?ad=cl55 – jhohman Apr 22 '13 at 21:02
  • Darn. Everything was working fine - the problem was I kept looking at the "view source" rather than "inspect element" in chrome. The hidden field was updated correctly, but I just wasn't doing the right thing to see it. – jhohman Apr 22 '13 at 21:18
  • @adeneo - are there any advantages to using the "$('#adtrack1').val(window.location.search.split('=')[1]);" other than there is less code on the page? – jhohman Apr 22 '13 at 21:22
  • I've made that mistake too. And no. It should work the same. The downside is if you decide later to change what is being passed in the query string. – sbatson5 Apr 22 '13 at 21:31

1 Answers1

1

try this $('input[name=meta_adtracking]').attr('value', adtest)

Austin Wang
  • 899
  • 9
  • 19