0

I cannot find the accept answer on here.

Currently I have a simple html form, that allows the user to enter text, in this case a user name.

        <form class="Find Friend">

        <div class="error" style="display:none"></div>
        <input type="text" id="friendsearch" placeholder="Find Friend" class="input-field" required/> 
        <button type="submit" class="btn btn-login">Find</button> 
        </form>

I want to capture that name in a variable for later use. Do I simply use ?

var findFriend = friendsearch;

2 Answers 2

1

To keep the var updating on each user input, you can use.

http://jsfiddle.net/gRZ7g/

var friendName;

$('#friendsearch').on('keyup', function(e) {
   friendName = $(this).val();
});

$('.show-value').click(function(e) {
   alert(friendName); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

Have to use keyup instead of keypress or keydown since the var gets set before the input gets a value on those.
1

You can get it like this:

var findFriend = $('#friendsearch').val();

You have to use the jQuery selector to select the element by its id.

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.