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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:30:01+00:00 2026-06-06T13:30:01+00:00

I am wondering if it is okay to put html dom references in js

  • 0

I am wondering if it is okay to put html dom references in js oop. Although there are many JS OOP tutorials online, I haven’t seen anything similar to what I am referring to. The code below is what I am referring to.

var form = {
  fname : document.getElementById("fname").value;
};

say, for example the document.getElementById("fname").value is a textbox with a value of “jacob”. would form.fname = "jacob"?

is this okay to do? is this a common practice to do?

  • 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-06T13:30:02+00:00Added an answer on June 6, 2026 at 1:30 pm

    I am not sure what you’re trying to accomplish here. If you only need to retrieve the value that is currently in the input field, one time, ever, then this ought to be fine. If you’re expecting the value of form.fname to continually update when the input value changes, you’re not going to see that happen. You’d either need some handler tied to the change event of the input, or you’d need to do something more like this:

    var form = {
      fname: function () {
        return document.getElementById('fname').value;
      }
    };
    
    // Retrieving
    form.fname()
    

    Note that you have to invoke the fname function now, you can’t simply refer to it as form.fname.

    If you really don’t want to ever have to retype things ever, do something like this:

    var fields = ['fname', 'lname', 'city', 'state'];
    var form = {};
    for (var i = 0, j = fields.length; i < j; ++i) {
      form[fields[i]] = function () {
        return document.getElementById(fields[i]).value;
      }
    }
    

    You’ll have to be a little bit more careful with that if you add things like dropdown lists to your form, because their .value isn’t as helpful, but you don’t retype names if one changes.

    And, if we really want to be fancy about it, you don’t even have to type names when you get the values:

    for (var i = 0, j = fields.length; i < j; ++i) {
      console.log(form[fields[i]]())
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if there is any way to get the lambda expressions that result from
Wondering if there is a good way to generate temporary URLs that expire in
Okay I was wondering when should I sanitize my code, when I add store
Okay so, I'm wondering how to unbind an inline onclick event in jQuery. You'd
Okay, I am wondering having clear web.config file could be good but you know
I'm wondering if it's considered okay (particularly, in Django) to have a URL that's
I'm wondering whether it's considered okay to do something like this. if ( p_Pointer
Okay, I'm new to blackberry development and I was wondering if anyone could give
Okay, so I've seen lots of solutions for styling checkboxes via CSS on the
Okay so it's sorta pointless considering that even if there was support for this

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.