0

Okay, so I have the following line of Javascript for which I'm trying to use to change the name attribute of an input element, following a click on a div element seperate from the input element.

Because I wish for the name attribute of the input element to change multiple times following separate clicks, I have attempted a for loop as seen below.

Javascript

document.getElementById('btn').addEventListener("click", function() {
  var letters = ["a", "b", "c", "d", "e"];
  for (var i = 1; i < letters.length; i++) {
    document.getElementById('textinput').name = letters[i];
    }
  });

This code is successful in changing the name attribute of the element with id textinput, however, it automatically changes it to e, and does not cycle through the for loop, despite however many clicks are performed.

I am unsure on the issue and have had difficulty finding a suitible solution here on stackOverflow, so any answers to this issue are deeply appreciated. Thanks in advance!

0

3 Answers 3

3

The for loop you have will cycle through entirely each time, meaning that it will always end on e.

Instead, you'll want to increment the letter clicked on each time, and ensure that if it reaches the end of the array, it cycles back around to the start.

var letters = ["a", "b", "c", "d", "e"];
var textInput = document.getElementById("textinput");
var i = 0;

document.getElementById("btn").addEventListener("click", function() { 
  var letter = letters[i];
  textInput.name = letter;
  console.log(letter);
  
  if (++i >= letters.length) i = 0;
});
<div id="btn">Click here!</div>
<input id="textinput">

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

Comments

3

Your entire for loop will be executed on every click, leaving the name attribute to the last value of your array ("e").

What you need is to increment the offset of your array at every click, something like:

var i = 0;
var letters = ["a", "b", "c", "d", "e"];
document.getElementById('btn').addEventListener("click", function() {
  i++;
  document.getElementById('textinput').name = letters[i];
});

1 Comment

This is Ok for some extent. but it will give error on fifth click.
0

To be more efficient, store your letters array, input element and index outside of the event handler:

var letters = ["a", "b", "c", "d", "e"];
var textinput = document.getElementById("textinput");
var index = 0;

To cycle through each element of the letters array, set the .name property using the value of the current index and increment it:

textinput.name = letters[index++];

To make sure you don't access the index of your array, that is undefined, use some boundary guard:

if (index >= letters.length) {
  index = 0;
}

var letters = ["a", "b", "c", "d", "e"];
var textinput = document.getElementById("textinput");
var index = 0;

document.getElementById("btn").addEventListener("click", function() { 
  textinput.name = letters[index++];    
  
  if (index >= letters.length) {
    index = 0;
  }
  
  console.log(textinput.name);
});
<input id="textinput" type="text"/>
<button id="btn">Click</button>

2 Comments

This is a great answer! Why add jQuery though?
Thanks @SamHolmes! Must have been a misplaced click in the code editor, corrected now.

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.