Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:28:20+00:00 2026-06-14T18:28:20+00:00

I need to use jQuery’s keyup function to take the value from input html

  • 0

I need to use jQuery’s keyup function to take the value from input html form elements and display said values in a div elsewhere.

The working code looks as follows:

$('#name').keyup(function() {
    var name = $(this).val();
    $('#name-in-document').html(name);
});

Since I have many identical instances of the above code block, I’d like to use a for loop to loop through an array of values. The catch is the name of the variable in the second line

var name = $(this).val();   

would come from the array.

I have tried the following loop, which does not work because (as I understand it) a Javascript variable cannot be named an array value:

var inputsArray = ["phone", "name", "address"];

for (var i = 0; i < inputsArray.length; i++) {

    $("#"+inputsArray[i]).keyup(function() {

    var inputsArray[i] = $(this).val();
    $("#"+inputsArray[i]+"-in-document").html(inputsArray[i]);

    })

};

So I have two questions:

  1. Is it true that I cannot use the array values to create a variable in the for loop?
  2. Is there an alternate way to accomplish the same thing (getting the variable names from the array) that might work?

I am just beginning JavaScript and really appreciate any insight. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:28:21+00:00Added an answer on June 14, 2026 at 6:28 pm

    For the alternative approach, I would recommend giving #name (and his friends) a class name, e.g.

    <input class="js-key-up" id="name" />
    

    Then you can do away with the array and the for loop altogether. Also, adding new HTML elements would not require adding items to the array.

    HTML

    <input class="js-key-up" id="phone">
    <input class="js-key-up" id="name">
    <input class="js-key-up" id="address">
    
    <p id="phone-in-document"></p>
    <p id="name-in-document"></p>
    <p id="address-in-document"></p>
    

    JavaScript
    ​

    $('.js-key-up').keyup(function (e) {
        var id = $(this).attr('id');
        $('#' + id + '-in-document').html($(this).val());
    });​
    

    I’ve created a jsfiddle with the code in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use jquery picnet table filter - http://www.picnet.com.au/picnet-table-filter.html I need to display count of
I need a way to use the jquery .live() function to act on elements
I need to use jQuery each function but also need it to work from
I need to use jquery to add and delete input fields based on user's
I think I need to use jQuery's index() function but I'm doing it all
I have a situation where I need to use jQuery's $.fn.one() function for a
I have been using mootools for a year now. I need to use jquery
I am need paint my image. I'm trying use JQuery in here this link:
I use a jquery validation plugin and I need to add some extra checking,
I'm stucking with jQuery where I need to use closest to get the .text()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.