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

  • Home
  • SEARCH
  • 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 321181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:49:19+00:00 2026-05-12T08:49:19+00:00

Some background: On a recent project, I tried to write a streamlined jQuery plugin

  • 0

Some background:

On a recent project, I tried to write a streamlined jQuery plugin that would handle some Ajax calls made when various inputs were updated. I wrote the JavaScript function as a plugin so that I could just call it on various inputs, like so:

$("#email").updateChanges();

Then, from within the plugin, I collected the input’s id, value, etc.

The problem:

Something that I really wanted to do, but couldn’t find a solution for, was to dynamically generate the name of the data variable being passed through ajax.

To be more clear, given this function:

jQuery.fn.updateChanges = function(){

   this.bind('blur',function(){

      var inputName = $(this).attr("name");
      var inputValue = $(this).val();

      $.post('/ajax/updateValue.php',
        { email: inputValue }, function(ret){
           if (ret=='success') alert("all good!");
        }

   }

}

…how do I present the data for the Ajax call as { password: inputValue } instead of { email: inputValue } when the inputName variable is “password” instead of “email”? This is a very specific example, but basically I’m just looking for a way to read the name of the data variable from a separate dynamic variable.

I tried window[inputName] with no luck, and I’m pretty much convinced this is impossible. However, if someone has an idea, I’d be very impressed.

Incidentally, we ended up going with { type: inputName, value: inputValue } instead, but it required a little bit more legwork on the PHP side (don’t ask me, I’m just the front-end guy :).

Thanks in advance!

  • 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-12T08:49:20+00:00Added an answer on May 12, 2026 at 8:49 am

    If I understand you correctly, this is what you’re after:

    jQuery.fn.updateChanges = function(){
    
       this.bind('blur',function(){    
    
          var data = {};
          data[$(this).attr("name")] = $(this).val();
    
          $.post('/ajax/updateValue.php',
            data,
            function(ret){
               if (ret=='success') alert("all good!");
            }    
       }    
    }
    

    Your main issue was attempting to use a dynamic value using the object literal notation, something you cannot do without reverting to really bad practices (like using eval() where eval() doesn’t belong ;).

    var myObj = { 'n1': 'v1' };
    

    is equivalent to:

    var myObj = {}; // {} = new Object();
    myObj['n1'] = 'v1';
    

    is equivalent to:

    var myObj = {},
        myKey = 'n1',
        myVal = 'v1';
    
    myObj[myKey] = myVal;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.