here is some HTML Code as example:
<button onclick="setLetter('A');">A</button>
<button onclick="setLetter('B');">B</button>
<button onclick="setLetter('C');">C</button>
<button onclick="setLetter('D');">D</button>
<button onclick="setLetter('E');">E</button>
<button onclick="setLetter('F');">F</button>
<button onclick="setLetter('G');">G</button>
<button onclick="setLetter('H');">H</button>
<button onclick="setLetter('I');">I</button>
<button onclick="setLetter('J');">J</button>
...
...
<div id="name"></div>
here the javascript part:
function setLetter(letter) {
document.getElementById('name').innerHTML = document.getElementById('name').innerHTML + letter;
}
This works perfectly fine, but has a lot of redundancy, which I want to remove basicly by using the for loop and the method String.fromCharCode()
. I just cant figure out how to do it.
Try this:
Try this.
HTML
Javascript
This solution like @Arvind's, but I think better use
attribute
of html tag and setonclick
withfunction
s.