1

HTML part

      <select class="data_type">
         <option value="inc" selected>+</option>
         <option value="exp" >-</option>
      </select>

      <div>
          <input type="text" placeholder="description" class="data_description">
          <input type="number" placeholder="value" class="data_value">
          <button class="add_btn">Add value</button>
      </div>

javascript part

var uiController = (function() {
    return { 
             inputVal : {
                          type:document.querySelector('.data_type').value,
                          description: document.querySelector('.data_description').value,
                          value:document.querySelector('.data_value').value
             } 
    };
})();

whenever the object returns and i display it in the console it doesn't read any value from the input box and displays nothing

ex output in console --> {type: "inc", description: "", value: ""}

it only read the inc which is specified in the select type but not the input values

any idea why this is happening ? it should work according to me.

4
  • Are you calling that code after any user input or only once when the page is loaded? Commented Sep 13, 2019 at 15:49
  • I think it needs onClick for the input? Commented Sep 13, 2019 at 15:51
  • As @VLAZ asked, when are you trying to capture these values? Commented Sep 13, 2019 at 16:14
  • i have set a event listener for add_btn to read whatever value i am giving in the input. So like im trying to input again and again whenever i press the button but everytime it just doesnt read any vlaues. Commented Sep 14, 2019 at 13:06

1 Answer 1

1

Values from inputs (and select) are read only once - during object construction. Make them into properties with getters.

var uiController = (function() {

  return {
    inputVal: {
      get type() { return document.querySelector('.data_type').value; },
      get description() { return document.querySelector('.data_description').value; },
      get value() { return document.querySelector('.data_value').value; }

    }
  };

})();

console.log(uiController);

function show_uiController() {
  console.log(uiController);
}
<select class="data_type">
  <option value="inc" selected>+</option>
  <option value="exp">-</option>
</select>

<div>
  <input type="text" placeholder="description" class="data_description">
  <input type="number" placeholder="value" class="data_value">
  <button class="add_btn">Add value</button>
</div>

<button onclick="show_uiController()">Show uiController</button>

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

Comments

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.