0

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

4
  • 1
    Try wrapping your click handler inside document.ready. -- $(function () { $("#btn-verify").click({ api_key = $("#verify").val(); }); }); Commented May 2, 2013 at 20:42
  • How does it not work? Commented May 2, 2013 at 20:42
  • @Justin I get "Unexpected token (" or "Unexpected token =" Commented May 2, 2013 at 20:43
  • @CQM I'm with Vega. It probably is executing before the the document is finished loading. Commented May 2, 2013 at 20:44

2 Answers 2

2

You are missing the function declaration when binding the handler.

Change

$("#btn-verify").click({

to

//Missing function declaration--v   
  $("#btn-verify").click(function (){
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.