Reputation: 44228
I set a variable 'api_key' on document load. I want to modify this variable with the text of an input field after clicking a button
var api_key = "";
//my button has an id named "btn-verify", the input field has an id named "verify"
$("#btn-verify").click({
api_key = $("#verify").val();
});
this doesn't work, how should I fix this. How do I assign api_key to the value of the input field, on click of a button
Upvotes: 0
Views: 5028
Reputation: 588
Try this:
$(document).ready(function(){
$("#btn-verify").click(function(){
api_key = $("#verify").val();
});
});
the ready function will cause JS to wait untill the DOM finishes to load before runing.
Upvotes: 0
Reputation: 79830
You are missing the function declaration when binding the handler.
Change
$("#btn-verify").click({
to
//Missing function declaration--v
$("#btn-verify").click(function (){
Upvotes: 2