/    Sign up×
Community /Pin to ProfileBookmark

Finding first (and last) ‘not null’ values in Array

Hi

First off, I am quite new to Javascript so please bear with me. I may not be going about this in the right way, so any advice would be appreciated.

I have included some test code I am working on below but here’s a quick summary of what i am trying to achieve…

I will have a set of 31 checkboxes, which the site visitor will check dependent on their respective choices. I want to store the choices in an array and if the visitor chooses checkbox 25 then a value is added to position 25 in the array. As the visitor selects/deselects various checkboxes the array is dynamically updated in length and value.

When the visitor has finished the array may have empty values and the first and last value of the array may or may not be empty.

I wish to detect the first and last ‘checked’ value in the array.

ie If the first checkbox selected is checkbox 4, then I want to know the value of that checkbox. Same with the last.

Here’s the sample code using 4 checkboxes. The values are arbitrary. The textfields are there simply for me to see what is happening as the checkboxes are selected/deselected.

As the code stands I am getting the first value (only if the first checkbox is selected) and the length of the array rather than the last value.

I guess I need to loop through the array again but I am unsure what to look for within the loop.

Can anyone help please…??

TIA

[CODE]<html>
<head>
<title>Checkbox test</title>

<script language=”javascript”>

function checkscript() {

var startDate;
var finishDate;

boxes = new Array();

for(i = 0; i < document.test.chk.length; i++){

if (document.test.chk[i].checked==true){

boxes[i] = document.test.chk[i].value;

}

}

startDate = boxes[0];
finishDate = boxes.length;

document.test.dates.value = boxes;
document.test.startDate.value = startDate;
document.test.finishDate.value = finishDate;

}

</script>

</head>
<body>
<form name=”test”>
1. <input type=”checkbox” onClick=”return checkscript()” value=”DAY1″ name=”chk”><br>
2. <input type=”checkbox” onClick=”return checkscript()” value=”DAY2″ name=”chk”><br>
3. <input type=”checkbox” onClick=”return checkscript()” value=”DAY3″ name=”chk”><br>
4. <input type=”checkbox” onClick=”return checkscript()” value=”DAY4″ name=”chk”><br>
<br>
Array (boxes): <input type=”text” name=”dates” size=”10″><br>
First Array Value: <input type=”text” name=”startDate” size=”10″><br>
Last Array Value: <input type=”text” name=”finishDate” size=”10″><br>
</form>

</body>
</html>[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@threezeroauthorJun 02.2003 — sorry, this just shows my extreme newbie nature when I say that I can't get that to work!! ?

I have replaced my code with yours and changed the 'formname' part to reflect the form name in the html, but alas, nothing...

I have however, received a reply on another forum which does what I was asking.

This is courtesy of tamago over at [URL=http://www.webxpertz.net/forums/showthread.php3?s=&postid=131278]WebXpertz forum[/URL], thought I'd share it here...

[CODE]function checkscript() {

boxes = new Array();
var haveStartDate = 0;

for(i = 0; i < document.test.chk.length; i++){

if (document.test.chk[i].checked){

//boxes[i] = document.test.chk[i].value;

var finishDate = document.test.chk[i].value;

if(!haveStartDate){
var startDate = document.test.chk[i].value;
haveStartDate = 1;
}
}
}

//document.test.dates.value = boxes;

document.test.startDate.value = startDate;
document.test.finishDate.value = finishDate;

}[/CODE]


if anyone has comments about it please feel free to jump in.
×

Success!

Help @threezero 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...