javascript - How to create maximum 8 <input type="button"> with value A, B, C and so on? -
i trying create, @ max, 8 buttons , successful @ that, want give values buttons a, b, c , on.. here code have done far:
var buttonvalue = 1; function addbutton(){ if(buttonvalue <=8){ var targetdiv = document.getelementbyid('targetdiv'); var inputfield = document.createelement('input'); inputfield.setattribute('type','button'); inputfield.setattribute('id',"input-"+buttonvalue+""); inputfield.setattribute('class','controls'); targetdiv.append(inputfield); } buttonvalue++; }
but want give values buttons a, b, c , on.
the character code a 65 ("a".charcodeat(0)). can use string.fromcharcode create values adding buttonvalue - 1 65:
inputfield.setattribute("value", string.fromcharcode(65 + buttonvalue - 1)); // or inputfield.defaultvalue = string.fromcharcode(65 + buttonvalue - 1); // or inputfield.value = string.fromcharcode(65 + buttonvalue - 1); // see below details on .value vs. .defaultvalue btw, can save fair bit of typing using reflected properties type, id, , class attributes:
inputfield.type = 'text'; inputfield.id = "input-" + buttonvalue; inputfield.classname = 'controls'; // ^^^^^^^^^ note slightly-different name (also note +"" unnecessary in id code.)
for value, it's bit complicated:
.value = ...sets current value of input, not same thingvalueattribute- the
valueattribute sets default value of input, not current value. - the reflected property value attribute
defaultvalue, notvalue
Comments
Post a Comment