0

Edited

Sorry if the title is not clear enough. I really don't know how to describe what I ask for without an example.

The code is below.

Is it possible to fix this code so that I can use "normal" text for innerHTML? That is, labels should be as follows:

Dislike
Neither like nor dislike
Like

const negative_attitude = ['dislike', 'Dislike'];
const neutral_attitude = ['neither-nor', 'Neither like nor dislike'];
const positive_attitude = ['like', 'Like'];

const radio_buttons = [negative_attitude[0], neutral_attitude[0], positive_attitude[0]];
const checked = 'neither-nor'

radio_buttons.forEach((radioButton) => {
  const input = document.createElement('input');
  input.type = 'radio';
  input.id = radioButton.toLowerCase();
  input.name = 'attitude';
  input.value = radioButton.toLowerCase();

  if (radioButton.localeCompare(checked) === 0) {
    input.checked = true;
  }

  const label = document.createElement('label');
  label.innerHTML = radioButton; // How to fix?
  label.htmlFor = radioButton.toLowerCase();

  document.body.appendChild(input);
  document.body.appendChild(label);
});

8
  • Why are you adding negative_attitude[0] to radio_buttons? Don't you want to add negative_attitude? Why do you even have radio_buttons at all? Why not just directly access negative_attitude? Commented Mar 9, 2021 at 20:13
  • 2
    You have an array where each element is a string; if you want an array of arrays you need to fix radio_buttons first. Commented Mar 9, 2021 at 20:13
  • 2
    Since you're putting the [0] element of each array into radio_buttons it will be ['dislike', 'neither-nor', 'like'] so you don't have an array of arrays. Commented Mar 9, 2021 at 20:13
  • 1
    I will edit question. Sorry, please wait for a few minutes. Commented Mar 9, 2021 at 20:18
  • 1
    @RocketHazmat Sorry couple of beers in me :D Seemed right Commented Mar 9, 2021 at 20:19

3 Answers 3

1

The problem is that your radio_buttons is not an array of arrays. It's an array of strings. When you use [0], you are getting the 0th (starting) element of the array, so you've just added 3 strings to radio_buttons.

You need to actually make an array of arrays, by adding the entire arrays to radio_buttons. Then when you loop over it, each element will have 2 elements ([0] and [1]) for you to work with.

const negative_attitude = ['dislike', 'Dislike'];
const neutral_attitude = ['neither-nor', 'Neither like nor dislike'];
const positive_attitude = ['like', 'Like'];

const radio_buttons = [negative_attitude, neutral_attitude, positive_attitude];
const checked = 'neither-nor'

radio_buttons.forEach((radioButton) => {
  const id = radioButton[0];
  const lbl = radioButton[1];

  const input = document.createElement('input');  
  input.type = 'radio';
  input.id = id;
  input.name = 'attitude';
  input.value = id;

  if (id.localeCompare(checked) === 0) {
    input.checked = true;
  }

  const label = document.createElement('label');
  label.innerHTML = lbl;
  label.htmlFor = id;

  document.body.appendChild(input);
  document.body.appendChild(label);
});
Sign up to request clarification or add additional context in comments.

2 Comments

A small side question. I see you use lbl to avoid a collision with label. I think this is just a quick-and-dirty solution and you have a better practice to handle such cases. Maybe and underscore or something like this, that is, to replace lbl with label_?
@johnc.j. Yes, I named it lbl, because you already had a variable called label. It's up to you how you want to name variables.
1
radio_buttons = [negative_attitude[1], neutral_attitude[1], positive_attitude[1]];
console.log(radio_buttons);

Is this what you want as i made the code as far i understood what you wanted

2 Comments

console.log(radio_buttons = [negative_attitude[1], neutral_attitude[1], positive_attitude[1]]);
No, sorry. It was my fault that the initial version of the question was confusing and misleading. It is now edited.
0

1. No need to iterate over the arrays in this case as radio_buttons actually outputs as a simple array of strings containing the following:

['dislike', 'neither-nor', 'like']

You don't actually have an array of arrays in other words.

2. Here is what you need with different indexes on radio_buttons to get what you need:

const radio_buttons = [negative_attitude[1], neutral_attitude[1], positive_attitude[1]];

3. However, as mentioned in the comments, you might as well just hard code an array, because it makes no difference and is simpler:

const radio_buttons = ['dislike', 'neither-nor', 'like']

3 Comments

negative_attitude[0] should be negative_attitude[1].
Thanks, i'll amend that now.
It was my fault that the initial version of the question was confusing and misleading. It is now edited. I cannot change negative_attitude[0] to negative_attitude[1] because the 0 elements are actually used for IDs and 1 elements are actually used for labels. If I change it in that way, I will end up with id="neither like nor dislike"

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.