Set the value for a hidden parameter from the URL using javascript
-
17-05-2021 - |
Question
I'm trying to set the hidden field for 'item_number' from the Url QueryString for a paypal form.
So the URL will look like this "http://website.com/customize.aspx?item_number=FFFF"
and code:
<script language="javascript" type="text/javascript">
document.getElementById('item_number').Value = Request.QueryString('item_number');
</script>
<input type="hidden" name="item_number" value="">
But this doesnt work for me. Whats wrong here???? is there a better way?
Solution
getElementById
only finds elements by their ID. Your hidden doesn't have the id
of item_number
; it has that name, however. If you add id="item_number"
to your input
, then the code should work. You also need to move your script to after the DOM element. Otherwise, it will run before the input
exists in the document.
Update
Just noticed another mistake. You're setting a Value
property, and Request.QueryString('item_number')
is also invalid. It looks like you're confusing ASP.NET code with JavaScript. The correct property name for the hidden input is value
(lowercase). There is no equivalent of Request.QueryString
in JavaScript. Rather, to extract query string values, see this answer for a good way to do so.