1

i'm trying to set an input value to a var named account after the user hits a button, I've tried this without jquery and everything checks out fine, but this is for an assignment and I must use jquery.

here is the html input and button:

<input type="" class="Re" id="Email1"/>
<button class="Re" id="Start" >Start</button>

Now i set the button to show an alert with the input value in the var account to check if it works, here is the code:

var account = "a";
$(document).ready(function(){
    $("#Start").click(function(){
        $("#Email1").val(account);
        alert(account);
    });
});

account it's set on "a" mainly for testing, the "a" doesn't seem to change nothing.

8
  • This sets the input element's value to a correctly. Are you sure you have included the jQuery javascript file into the project? Commented Nov 7, 2018 at 21:58
  • Yes i'm sure, that's not the only jquery code on my project and all code runs as it should be, i'm trying to set whatever the user puts in the input to the var account, not the other way around. Commented Nov 7, 2018 at 22:03
  • It works for me: jsfiddle.net/barmar/x40gvasL/1 After I click the button, the input field is filled in with a. Commented Nov 7, 2018 at 22:07
  • If im understanding correctly, you just want account = $("Email1").val() Commented Nov 7, 2018 at 22:08
  • Have you checked your web dev console? Commented Nov 7, 2018 at 22:09

2 Answers 2

1

If you want to set the value of the account variable to the form field's value, call $.val() without arguments:

var account = "a";
$(document).ready(function(){
    $("#Start").click(function(){
        account = $("#Email1").val();
        alert(account);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to set account to $("#Email1").val();

var account = "a";

$(document).ready(function() {
  $("#Start").click(function() {
    account = $("#Email1").val();
    alert(account);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="" class="Re" id="Email1" />
<button class="Re" id="Start">Start</button>

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.