change all value of inputs with same id in javascript

How can I change all value of same id inputs?

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

I use

<script>
document.getElementById("secure").value = '123456';
</script>

But only first one changes.

<input type="hidden" id="secure" name="secure" value="123456">
<input type="hidden" id="secure" name="secure" value="">
<input type="hidden" id="secure" name="secure" value="">
<input type="hidden" id="secure" name="secure" value="">

Answers:

Answer

You need to change id="secure" to class="secure" first

HTML:

<input type="hidden" class="secure" name="secure" value="">
<input type="hidden" class="secure" name="secure" value="">
<input type="hidden" class="secure" name="secure" value="">
<input type="hidden" class="secure" name="secure" value="">

JavaScript:

<script>
var els = document.getElementsByClassName("secure");
Array.prototype.forEach.call(els, function(el) {
    // Do stuff here
    el.setAttribute('123456');
});
</script>
Answer

IDs should be unique - that's the point of them. Classes are for the opposite use, i.e. where there may be multiple instances of a single one.

Assumimg you change your IDs to classes, we can do the following:

[].forEach.call(document.querySelectorAll('.secure'), function(el) {
    el.value = '123456';
});
Answer

Don't use same id multi-pal times. Use class rather then id. To see result I remove type="hidden" from your HTML.

var list = document.getElementsByClassName("secure");
for (var i = 0; i < list.length; i++) {
 list[i].setAttribute("value", "12345");
}
<input name="secure" value="" class="secure" />
<input name="secure" value="" class="secure" />
<input name="secure" value="" class="secure" />
<input name="secure" value="" class="secure" />

Answer

This works - need to reference by class and then loop through all the elements to actually change the value.

<div><input type="text" class="secure" id="secure1" name="secure" value="123456"></div>
<div><input type="text" class="secure" id="secure2" name="secure" value="123456"></div>
<div><input type="text" class="secure" id="secure3" name="secure" value="123456"></div>
<div><input type="text" class="secure" id="secure4" name="secure" value="123456"></div>

<script>
function myFunction() {
    x=document.getElementsByClassName("secure");
		for (i = 0; i < x.length; i++) {
    	x[i].value="100";
    }
}
</script>

<button onclick="myFunction()">
click me
</button>

Answer

You need to add a class for each elements as in a document duplicate id's are not allowed.So we can better keep a common class of our element [also I changed your elements id's for keeping unique]. getElementsByClassName() returns a nodeList of the DOM. We iterate over the element and put some value. Finally for testing I print value by id.

<input type="hidden" id="secure1" name="secure" value="" class="secure">
<input type="hidden" id="secure2" name="secure" value="" class="secure">
<input type="hidden" id="secure3" name="secure" value="" class="secure">
<input type="hidden" id="secure4" name="secure" value="" class="secure">

<script>
var list = document.getElementsByClassName("secure");
for(var i = 0; i< list.length;i++){
	document.getElementsByClassName("secure")[i].value='123456'
}
console.log(document.getElementById('secure2').value);
</script>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.