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 660241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:08:03+00:00 2026-05-13T23:08:03+00:00

I have an HTML form where I am going to copy values from a

  • 0

I have an HTML form where I am going to copy values from a series of input fields to some spans/headings as the user populates the input fields. I am able to get this working using the following code:

$('#source').keyup(function(){
    if($("#source").val().length == 0){
       $("#destinationTitle").text('Sample Title');
    }else{
      $("#destinationTitle").text($("#source").val());
    }  
  });

In the above scenario the html is something like:

Sample Title

Basically, as the users fills out the source box, the text of the is changed to the value of the source input. If nothing is input in, or the user deletes the values typed into the box some default text is placed in the instead. Pretty straightforward. However, since I need to make this work for many different fields, it makes sense to turn this into a generic function and then bind that function to each ‘s onkeyup() event. But I am having some trouble with this. My implementation:

function doStuff(source,target,defaultValue) {

  if($(source).val().length == 0){
       $(target).text(defaultValue);
  }else{
      $(target).text($(source).val());
  }  

}

which is called as follows:

$('#source').keyup(function() {
            doStuff(this, '"#destinationTitle"', 'SampleTitle');
  });

What I can’t figure out is how to pass the second parameter, the name of the destination html element into the function. I have no problem passing in the element I’m binding to via “this”, but can’t figure out the destination element syntax.

Any help would be appreciated – many thanks!

  • 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-05-13T23:08:04+00:00Added an answer on May 13, 2026 at 11:08 pm

    Call it like this:

    $('#source').keyup(function() {
            doStuff(this, '#destinationTitle', 'SampleTitle');
    });
    

    It’s just a string 🙂

    If the element is in relation to the other though, you can make it generic, for example if #destinationTitle is always in the next div, next table cell, etc…always in the same relative place, you can make this cleaner for all keybinds, if you post some sample markup I’ll show you how.

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

Sidebar

Related Questions

i have a input tag which is non editable, but some times i need
I have a script that appends some rows to a table. One of the
I have a login.jsp page which contains a login form. Once logged in the
We manage a site for a medical charity. They have a number of links
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
This is beyond both making sense and my control. That being said here is

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.