2

I have an input in html and want to put the text of the input into an object on JS and display it on a

tag on html as a result. Because I didnt wanted to simplify the code, i tried to create a function (so that I can call it for each object attribute. <input type="text" placeholder="Typ" id="type-mhouse" /> Is the input.

const land = {
  Type: "",
  Adress: "",
  Value: "",
  updateValue: function (x, y, z) {
    this.x = document.getElementById(y).value;
    document.getElementById(z).textContent = this.x;
    console.log(this[0]);
  },
};

Is the JS.

<p class="ergebnis" id="ergtype-house"></p>

Is where the result is displayed.

The problem is: when I type for example "Flat" in the input, it displays properly on the html output, but the land.Type remains "".

Ive tried everything and I'm still very new to JS so I cant solve it. (Also I know that I shouldnt name stuff "Type" or "Value".. I'll change it.)

Thanks for any comment!

IMPORTANT Edit: The land object is used in a linked js file. The function is called inside the HTML on a button click:

<script>
        $(document).ready(function () {
          
          //Button on Modal
          $("#submit").click(function () {
           
            land.updateValue("Type", "type-mhouse", "ergtype-house");

          });
        });
      </script>
6
  • Where are you using land object? Commented Oct 31, 2021 at 11:46
  • In a separate script.js file, linked to the html. However the button that confirms the change is stored in a JSON script on the html Commented Oct 31, 2021 at 11:49
  • Please post the whole code. Commented Oct 31, 2021 at 11:49
  • @youngmago where are you changing the land.Type value? Please post your whole code for us to help. Commented Oct 31, 2021 at 11:51
  • Sorry, i edited it now and posted the code. Thanks for helping ! Commented Oct 31, 2021 at 11:53

1 Answer 1

1

I'm not sure to understand what you try to do but here is a solution to fix your issues.

const land = {
  type: "",
  address: "",
  value: "",
  updateValue: function(landAttributeName, textElementId, displayElementId) {
    this[landAttributeName] = document.getElementById(textElementId).value;
    document.getElementById(displayElementId).innerText = this[landAttributeName] || '';
  },
};

BTW, you're right, you should use names more easily understandable :-)

There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the detailed answer and the quote! You saved me from more days of headache. It works!! I wanted to modify the object attributes with an input and display them on the page by button click.
Thanks again!! If you have the time could you explain to me in short what i did wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.