I want to set a button’s id to a variable, so that I can easily get access to changing numbers of buttons:
<script>var test = 1; </script>
<button id=test> click me </button>
but it is does not work.
my motivation to do this :
I read from a xml file and display part of them using form. and then I have to change the value of them. and the number of items I read from xml is not fixable. so if i want to control them i need give each item a number.
Your var ‘button’ will contain the DOM object with the id ‘test’. You might wanna do this a little more ‘dynamic’ something like:
This way, you simply need to add “class=’likedButton'” to your button and this will add all those button into the array ‘myArray’. Next step if you wanna do anything to those button, just go throught the ‘myArray’ object myArray[0] showing the first object with the class ‘likedButton’. I suggested the class attribute so that you can easily find tons of button. If you use the ID attribute, all IDs MUST be different.