/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Create an array from clicked check boxes

I am trying to make an array from the check boxes that were clicked.

I want to append ID’s to the end of an array if the user clicks on a check box. If the ID is already in the array, I would like to remove that ID from the array instead of appending it. Anyone know how I can do this?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@bals28mjkOct 19.2007 — Well, it pretty much sounds like you know what you want to do. What part are you confused about?
Copy linkTweet thisAlerts:
@KorOct 19.2007 — Instead of pushing/pulling elements in an array, you may build that array each time when a checkbox is clicked. Could somerhing like
<i>
</i>var chID=[];
var allINP=document.getElementsByTagName('input');
var i=0, inp;
while(inp=allINP[i++]){
inp.type=='checkbox'&amp;&amp;inp.checked?chID[chID.length]=inp.id:null;
}
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 21.2007 — OK, I have this... Now what do I do with it?

function fileToDelete(id,thisid){
var chID=[];
var allINP=document.getElementsByTagName('input');
var i=0, inp;
while(inp=allINP[i++]){
inp.type=='checkbox'&amp;&amp;inp.checked?chID[chID.length]=inp.id:null;
}
alert(allINP[0]);
}


now... I want to place hidden fields between the span. The value of the hidden field should be the id# of the file in the database, the check boxes were created by the following:

[code=php]echo '<input type="checkbox" id="cBox_'.$rows['file_id'].'" onclick="fileToDelete(''.$rows['file_id'].'',this.id)" />';[/code]

The span can be found here:
[code=php]
echo '<form action="file_action.php" method="post">';
echo '<input type="submit" name="delete" value="Delete Selected" />
<span id="files_to_delete"></span>
</form>';
[/code]


Does that make sense?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorOct 21.2007 — Thank You SO MUCH... after a little messing around, I got it!
Copy linkTweet thisAlerts:
@JMRKEROct 21.2007 — Sorry, been away for awhile. You beat me by a bit on a solution.

Here's what I did with it:
[code=php]
<html>
<head>
<title>CheckBox Array</title>
<script type="text/javascript">
function AdjArray() {
var chID=[];
var allINP=document.getElementsByTagName('input');
var i=0, inp;
var strON = '';
var strOFF = '';
while(inp = allINP[i]){
if ((inp.type == 'checkbox') && (inp.checked))
{ strON += allINP[i].value+' '; } else { strOFF += allINP[i].value+' '; }
i++;
}
return 'There are '+allINP.length+' input elementsnON: '+strON+'nOFF: '+strOFF;
// alter 'return' contents for desired result

}
</script>
</head>
<body>
<input type="checkbox" id="cb1" value="CB1"'>Checkbox 1
<input type="checkbox" id="cb2" value="CB2"'>Checkbox 2
<input type="checkbox" id="cb3" value="CB3"'>Checkbox 3
<input type="checkbox" id="cb4" value="CB4"'>Checkbox 4
<input type="checkbox" id="cb5" value="CB5"'>Checkbox 5
<button onClick="alert(AdjArray())">Show Settings</button>
</body>
</html>
[/code]
×

Success!

Help @The_Little_Guy 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,
)...