/    Sign up×
Community /Pin to ProfileBookmark

getting the values of dynamically created checkboxes

As the title says really. How can i get the values of the checkboxes which are checked from a dynamically created group of check boxes.

bit stuck on this one…… I have this code:

any help would be great.

[CODE]

<html>
<head>
<script language=”Javascript”>

var array = new Array();
array.push(“foo”);
array.push(“bar”);
array.push(“test”);
array.push(“one”);

function createCheckBoxes()
{
document.getElementById( ‘append’ ).innerHTML = “”;
for(var i=0; i<array.length; i++) {

var divTag = document.createElement(“div”);
divTag.className =”filterCheckBoxes”;
divTag.id = “filterCheckBoxes”;

var cb = document.createElement( “input” );
cb.type = “checkbox”;
cb.id = “checkBoxes”+i;
cb.checked = true;
cb.value = array[i];

document.getElementById(“append”).appendChild(divTag);
divTag.appendChild(cb);
divTag.appendChild(document.createTextNode(array[i]));

}
}

function alertChecked()
{

for (var i=0;i<array.length;i++) {
if(document.getElementById(“filterCheckBoxes”).checked == true){
alert(document.getElementById(“filterCheckBoxes”).firstChild);
}

}

}

</script>
</head>

<body>
<p>click the button below</p>
<div id=”append” name=”append”></div>

<input type=”button” value=”createCheckBoxes” onclick=”javascript:createCheckBoxes()” />
<input type=”button” value=”test” onclick=”javascript:alertChecked()” />

</body>
</html>

[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 17.2008 — An id must be unique, so move #filterCheckBoxes out of the loop.

Form controls require a name to function correctly.

Change function alertChecked accordingly.
Copy linkTweet thisAlerts:
@PalliauthorOct 17.2008 — I see. Somthing like this then.....


<script language="Javascript">

var array = new Array();
array.push("foo");
array.push("bar");
array.push("test");
array.push("one");





function createCheckBoxes()

{

document.getElementById( 'append' ).innerHTML = "";

for(var i=0; i<array.length; i++) {

var divTag = document.createElement("div");
divTag.className ="filterCheckBoxes";


var cb = document.createElement( "input" );
cb.type = "checkbox";
cb.id = "checkBox"+i;
cb.name = "checkBox";
cb.checked = true;
cb.value = array[i];

document.getElementById("append").appendChild(divTag);
divTag.appendChild(cb);
divTag.appendChild(document.createTextNode(array[i]));


}
}


function alertChecked(){

for (var i=0;i<array.length;i++) {
var checkBoxObject = document.getElementById('checkBox'+i);
if(checkBoxObject.checked == true)
alert(checkBoxObject.value);
}
}

</script>
×

Success!

Help @Palli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...