Dano007
Dano007

Reputation: 1932

Add form input to variable

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;

Upvotes: 0

Views: 52

Answers (2)

Christopher Marshall
Christopher Marshall

Reputation: 10736

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); 
});

Upvotes: 1

gitsitgo
gitsitgo

Reputation: 6769

You can get it like this:

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

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

Upvotes: 1

Related Questions