0

I want to create inputs with javascript and read the value of it. it works fine as long as the input isn't disabled. But the user shouldn't be able to edit the input. How can I disable the inputs without making the value invisible to to javascript?

let input = document.createElement('input');
input.name = "tags[]";
input.value = data;
input.disabled = true;
myNode.appendChild(input);
var inputs = document.querySelectorAll("input[name='tags[]']");
  const active = [];
  for (i = 0; i < inputs.length; i++) {
    active.push(inputs[i].value.toLowerCase());
  }

1 Answer 1

1

You can use readOnly instead of disabled

for (let i = 0; i <= 10; i++) {
  let input = document.createElement('input');
  input.name = "tags[]";
  input.value = i;
  input.readOnly = true;
  document.getElementById('divid').appendChild(input);
}

var inputs = document.querySelectorAll("input[name='tags[]']");
const active = [];
for (i = 0; i < inputs.length; i++) {
  active.push(inputs[i].value.toLowerCase());
}
console.log(active);
<div id='divid'></div>

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.