1

I am using JS to set multiple forms which have the following hidden input.

<input type="hidden" name="sGUID" id="sGUID" value="">

Using my JS function I use

document.getElementById("sGUID").value = "my value goes here";

The problem is this only sets the last instance of the sGUID to the value set. How can I ensure all of them are set to this?

5
  • 1
    That's because IDs must be unique. Perhaps use classes instead and iterate over them. Commented Jun 26, 2019 at 15:43
  • ids are supposed to be unique throughout the entire document. use a class or an attribute like data-id instead Commented Jun 26, 2019 at 15:43
  • Ids should be unique... use class names instead Commented Jun 26, 2019 at 15:43
  • How do you have multiple ids? You cant have multiple ids and try to access all. you will have problem Commented Jun 26, 2019 at 15:43
  • Why don't you set class names to the inputs and try with getElementsByClassName()? Commented Jun 26, 2019 at 15:45

4 Answers 4

3

Use classes as IDS must be unique and loop through them.

let guids = document.getElementsByClassName("sGUID");
let newstring = "string";
for(let z = 0;z < guids.length;z++){
   guids[z].value = newstring;
}
<input type="hidden" name="sGUID" class="sGUID" value="">
<input type="hidden" name="sGUID" class="sGUID" value="">
<input type="hidden" name="sGUID" class="sGUID" value="">
<input type="hidden" name="sGUID" class="sGUID" value="">

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

1 Comment

Note: It's a bad practice to use .length directly on the comparison. Why ? Because here, .length is executed on every iteration (not good for performance). And if you change the length of the array you can create an infinite loop ! It's better to create another variable and put the length into.
2

You can use querySelectorAll and loop to change all inputs value

let inputs = document.querySelectorAll('input[name="sGUID"]');
for(let l = inputs.length;l--;){
   inputs[l].value = "my value goes here";
}

Comments

1

You can't have the same id value.

If you have sGUID as name values, try the below:

var sGUIDs = document.getElementsByName("sGUID");

for (var i = 0; i < sGUIDs.length; i++) {
    sGUIDs[i].value = "my value goes here";
}
<input type="hidden" name="sGUID" value="1">
<input type="hidden" name="sGUID" value="2">
<input type="hidden" name="sGUID" value="3">

1 Comment

Note: It's a bad practice to use .length directly on the comparison. Why ? Because here, .length is executed on every iteration (not good for performance). And if you change the length of the array you can create an infinite loop ! It's better to create another variable and put the length into.
1

This is actually a rewrite of @zmag's response, just in shorthand.

document.getElementsByName('sGUID').forEach(function(ele, idx) {
   ele.value = 'TESTVALUE';
})

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.