I would like to know if it is possible to loop through all the textboxes on a page and insert a value into each one starting with the number 1 and going up by +1. There are over 150 textboxes on this page and they are not in an array and I would not like to rename them one by one.. :-(
Thanks for any help....
I would like to know if it is possible to loop through all the textboxes on a page and insert a value into each one starting with the number 1 and going up by +1. There are over 150 textboxes on this page and they are not in an array and I would not like to rename them one by one.. :-(
Thanks for any help....
Share Improve this question asked May 27, 2011 at 21:42 lukeluke 131 silver badge3 bronze badges 1- "rename" or "set a value", what should it be? – Tomalak Commented May 27, 2011 at 21:49
3 Answers
Reset to default 5var input = document.getElementsByTagName("INPUT");
var j = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].type == "text") {
input[i].value = ++j;
}
}
function FillTextBoxes()
{
var tbs = document.getElementsByTagName("input");
var valCount = 0;
for (var i = 0 ; i < tbs.length ; i++)
{
if (tbs[i].type == "text")
{
tbs[i].value = ++valCount;
}
}
}
window.onload = FillTextBoxes;
Try this out:
$('input[type="text"]').each(function(index) {
$(this).val(index);
})
Per the request of some, for pleteness' sake, the above requires the JavaScript library, jQuery. You can reference it like so:
<script src="//ajax.aspnetcdn./ajax/jQuery/jquery-1.6.1.js"></script>
If you aren't using <!DOCTYPE html>
you can include the type="text/javascript"
attribute.