0

I have this simplified code:

const chancesData = ["facebook","twitter","google"];
        let inputsArr = [];
        const inputsArrFill = chancesData.map((cur, index) => {
          const input = (document.createElement("input").value =
            chancesData[index]);
          inputsArr.push(input); //I want the pushed value to be something like this "<input value"facebook">"
          return inputsArr;
          //so the returned array (should) be something like this: 
          //["<input value"facebook">","<input value"twitter">","<input value"google">"]
        });
        console.dir(inputsArrFill);
  

as you can see if you opened this coed in your console, the returned array looks like this:

['facebook', 'twiiter', 'google']

I want it to look like this:

['<input value="facebook">','<input value="twitter">','<input value="google">']

3 Answers 3

4

You can use setAttribute to put the value into the HTML markup, then retrieve it with outerHTML:

const chancesData = ["facebook", "twitter", "google"];
const inputsArrFill = chancesData.map(str => {
  const input = document.createElement("input");
  input.setAttribute('value', str);
  return input.outerHTML;
});
console.dir(inputsArrFill);

But it'd be easier to use ordinary string concatenation:

const chancesData = ["facebook", "twitter", "google"];
const inputsArrFill = chancesData.map(str => `<input value="${str}">`);
console.dir(inputsArrFill);

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

2 Comments

Why you're using setAttribute instead of .value ?
Because just setting the .value won't be reflected in the HTML markup which .outerHTML retrieves (just like typing into an input field doesn't change the HTML markup - it only changes the state of the element)
3

The result of an assignment expression, is the assigned value. With

const input = (document.createElement("input").value =
        chancesData[index]);

you did not store a reference to the created HTML element into your variable, you stored the assigned value, chancesData[index].

You need to do these two things in separate steps.

const chancesData = ["facebook","twitter","google"];
        let inputsArr = [];
        const inputsArrFill = chancesData.map((cur, index) => {
          const input = document.createElement("input");
          input.value = chancesData[index];           
            
          inputsArr.push(input); //I want the pushed value to be something like this "<input value"facebook">"
          return inputsArr;
          //so the returned array (should) be something like this: 
          //["<input value"facebook">","<input value"twitter">","<input value"google">"]
        });
        console.dir(inputsArrFill);

But if you really want <input value="facebook"> as a result, then you will probably have to push that in string form to begin with. You have little control over how an HTML element object gets displayed by the console.

1 Comment

Thanks a lot "The result of an assignment expression, is the assigned value."
1

Please refer below code.

(function createHTMlString() {
  const chancesData = ["facebook", "twitter", "google"];
  var outputObj = []
  for (var i = 0; i < chancesData.length; i++) {
  var inputTag = document.createElement("INPUT");
    inputTag.setAttribute("value", chancesData[i]);    
    outputObj.push(inputTag);
  }
  console.log(outputObj)
})();

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.