Reputation: 214
Learning HTML and JavaScript, I'm trying to form a custom URL for an API request, based on input submitted via a form. I've been able to craft most of the URL, but I'm having an issue getting the URL string to concatenate past the '?' when submitting the action. I made it this far based on documentation and forum posts alone, but cannot figure out how to complete this action and just wanted to ask someone.
<div id="stock_quote_iex">
<form id="get_stock_quote_form" onsubmit="get_stock_quote()" method="GET">
<p>Enter Symbol and API Key</p>
<input id="symbol" type="text" placeholder="Symbol">
<input id="api_key" type="text" placeholder="API_KEY">
<input type="submit" value="Submit">
</form>
</div>
<script language="javascript" type="text/javascript">
function get_stock_quote()
{
var form = document.getElementById('get_stock_quote_form')
var symbol = document.getElementById('symbol').value
var api_key = document.getElementById('api_key').value
var action_src = "https://sandbox.iexapis.com/stable/stock/" + symbol + "/quote?token=" + api_key
form.action = action_src;
}
</script>
Example inputs: symbol = NFLX API_KEY = pk_s0m3rand0mphra5e Expected output: https://sandbox.iexapis.com/stable/stock/NFLX/quote?token=pk_s0m3rand0mphra5e Actual output: https://sandbox.iexapis.com/stable/stock/NFLX/quote?
Upvotes: 1
Views: 1473
Reputation: 214
Corrected by removing the query parameter token
from the string concatenation, and using it in the name
attribute on api_key
instead.
<div id="stock_quote_iex">
<form id="get_stock_quote_form" onsubmit="get_stock_quote()" method="GET">
<p>Enter Symbol and API Key</p>
<input id="symbol" type="text" placeholder="Symbol">
<input id="api_key" name="token" type="text" placeholder="API_KEY">
<input type="submit" value="Submit">
</form>
</div>
<script language="javascript" type="text/javascript">
function get_stock_quote()
{
var form = document.getElementById('get_stock_quote_form')
var symbol = document.getElementById('symbol').value
var action_src = "https://sandbox.iexapis.com/stable/stock/" + symbol + "/quote"
form.action = action_src;
}
</script>
Upvotes: 1