had a look for a guide on this but couldn't really find anything specific on it, and thought you guys could help.
I have a javascript file which performs various tasks onLoad. Currently I have 5 buttons in an HTML page but what I would like to do is to is read from an array in the js file and dynamically create a number of buttons depending what was in the array.
Similarly I want the buttons on click to be 'listened' for (alter an array in the js)
I have an idea that I want to read the array elements and set them as the button IDs and create them. Likewise I want to listen to what button has been clicked by its ID and have it alter an array. But how do I actually go about that process?
Cheers
had a look for a guide on this but couldn't really find anything specific on it, and thought you guys could help.
I have a javascript file which performs various tasks onLoad. Currently I have 5 buttons in an HTML page but what I would like to do is to is read from an array in the js file and dynamically create a number of buttons depending what was in the array.
Similarly I want the buttons on click to be 'listened' for (alter an array in the js)
I have an idea that I want to read the array elements and set them as the button IDs and create them. Likewise I want to listen to what button has been clicked by its ID and have it alter an array. But how do I actually go about that process?
Cheers
Share Improve this question asked Aug 15, 2011 at 13:52 myolmyol 9,93624 gold badges97 silver badges158 bronze badges 6- Can you explain exactly what you want to do when the button is clicked? – DaveRandom Commented Aug 15, 2011 at 13:56
- If I have an array; arr = [0,1,2,4] I want to create buttons with those IDs, and once the buttons are created, I want to then change a different array depending if the button has been clicked; For example a button with ID = 2 and the arr2 = [0,1,3] when clicked would then change that array to arr2 = [0,1,2,3] – myol Commented Aug 15, 2011 at 14:08
- 2 Excellent, I took a wild guess and did just that in my solution. It doesn't behave like a set though... If you click the button twice, it will be added to arr2 twice. Is that what you want? Or do you want it to be added just once? – Spycho Commented Aug 15, 2011 at 14:11
- 2 My solution below is essentially a JQuery-free version of what @Spycho has done, so take you pick - I'd probably go with Spycho's cos JQuery will make your life much easier in the long run... – DaveRandom Commented Aug 15, 2011 at 14:16
- 1 Ah ok. I would go for @DaveRandom's solution then, but with a slight modification to toggle rather than continuously append. – Spycho Commented Aug 15, 2011 at 14:27
3 Answers
Reset to default 2Something like this...?
<html>
<head>
<script type="text/javascript">
var arrayToModify = [];
window.onload = function () {
var i, buttonsToCreate, buttonContainer, newButton;
buttonsToCreate = ['button1','button2','button3','button4','button5'];
buttonContainer = document.getElementById('this_element_contains_my_buttons');
for (i = 0; i < buttonsToCreate.length; i++) {
newButton = document.createElement('input');
newButton.type = 'button';
newButton.value = buttonsToCreate[i];
newButton.id = buttonsToCreate[i];
newButton.onclick = function () {
alert('You pressed '+this.id);
arrayToModify[arrayToModify.length] = this.id;
};
buttonContainer.appendChild(newButton);
}
};
</script>
</head>
<body>
<div id='this_element_contains_my_buttons'></div>
<input type='button' onclick='alert(arrayToModify);' value='Show the buttons I have clicked' />
</body>
</html>
EDIT: I have just added some functionality to track button presses in an array, as requested in the ment above
I would advise you use jQuery for this. It would make it a lot simpler.
Firstly, to create the buttons from an array of button ids you could do something along the lines of the following:
var buttons = ['start', 'stop', 'foo', 'bar'];
for(var i = 0; i < buttons.length; i++){
$('<button>')
.attr('id', buttons[i])
.text(buttons[i])
.appendTo('div');
}
Next, to listen for the button clicks, you could modify the above as follows:
var buttons = ['start', 'stop', 'foo', 'bar'];
var clicks = [];
for(var i = 0; i < buttons.length; i++){
$('<button>')
.attr('id', buttons[i])
.text(buttons[i])
.appendTo('div')
.click(function(){
clicks.push(this.id);
});
}
I'm not quite sure what you want to do on-click. Could you elaborate?
Here's a jsFiddle demonstrating the solution.
Use the event
property of the event listener to get the target element, then it's ID:
foo.onclick = function(event) {
alert(event.target.id);
}