/    Sign up×
Community /Pin to ProfileBookmark

arrays and loops!

Hi

i have created a function which attempts to cycle through an array until a certain condition is met. the array contains a list of ids which corrospond to checboxes on my form. i want an action to happen when one of those checkboxes is checked.

here is my code so far:

[COLOR=”Blue”]function listFaults() {

listHtml = “”;
var faultsArray = Array();
faultsArray = {FAULTS_ARRAY};

//think need some sort of loop here???

if(document.getElementById(“Repaired”+insert id value+).checked == true) {
listString = “checked”;
listHtml += “<li>”;
listHtml += “some text to say which box is checked”;
listHtml += “</li>”;
}

return listHtml;

} [/COLOR]

thanks alot

Paul

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameJan 09.2009 — Something like the following what you want?

[CODE]function listFaults(idPrefix,idArr) {
var listHtml = "", i = 0, idStr;
for (i = 0; i < idArr.length; i++) {
idStr = idPrefix + idArr[i];
if(document.getElementById(idStr).checked == true) {
listHtml += '<li>';
listHtml += 'html element with id of "' + idStr + '" has been checked!';
listHtml += '</li>';
}
}
return listHtml;
}[/CODE]



this would be a way to call it now (or just pass the id array in from a variable):

[CODE]listFaults('myIdPrefix_',['hasThisAddedToItInTheFunction', 'alsoHasThisAddedToItTemporarily', 'inTheForLoop']); [/CODE]
Copy linkTweet thisAlerts:
@paulnicauthorJan 09.2009 — Something like the following what you want?

[CODE]function listFaults(idPrefix,idArr) {
var listHtml = "", i = 0, idStr;
for (i = 0; i < idArr.length; i++) {
idStr = idPrefix + idArr[i];
if(document.getElementById(idStr).checked == true) {
listHtml += '<li>';
listHtml += 'html element with id of "' + idStr + '" has been checked!';
listHtml += '</li>';
}
}
return listHtml;
}[/CODE]



this would be a way to call it now (or just pass the id array in from a variable):

[CODE]listFaults('myIdPrefix_',['hasThisAddedToItInTheFunction', 'alsoHasThisAddedToItTemporarily', 'inTheForLoop']); [/CODE][/QUOTE]



hi thanks for your reply, i am getting document.getelementbyId(idStr) is null error?

when i hover over it in firebug it is saying NaN...

thanks

Paul
Copy linkTweet thisAlerts:
@paulnicauthorJan 09.2009 — heres my function now:

[COLOR="Blue"]function listFaults(idPrefix,idArr)

{

var idArr = Array();

idArr = {FAULTS_ARRAY};



var listHtml = "", i = 0, idStr;
for (i = 0; i < idArr.length; i++) {
idStr = idPrefix + idArr[i];
if(document.getElementById(idStr).checked == true) {
listHtml += '<li>';
listHtml += 'html element with id of "' + idStr + '" has been checked!';
listHtml += '</li>';
}
}
return listHtml;

}[/COLOR]
Copy linkTweet thisAlerts:
@astupidnameJan 09.2009 — The following works (no different from what I previously posted):

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
<script type="text/javascript">

function listFaults(idPrefix,idArr) {
var listHtml = "", i = 0, idStr;
for (i = 0; i < idArr.length; i++) {
idStr = idPrefix + idArr[i];
if(document.getElementById(idStr).checked == true) {
listHtml += '<li>';
listHtml += 'html element with id of "' + idStr + '" has been checked!';
listHtml += '</li>';
}
}
return listHtml;
}

</script>
</head>
<body>
<div>
<form action="">
<input id="myIdPrefix_hasThisAddedToItInTheFunction" type="checkbox">
<input id="myIdPrefix_alsoHasThisAddedToItTemporarily" type="checkbox">
<input id="myIdPrefix_inTheForLoop" type="checkbox"><br>
<input type="button" onclick="alert(listFaults('myIdPrefix_',['hasThisAddedToItInTheFunction', 'alsoHasThisAddedToItTemporarily', 'inTheForLoop']));" value="See listHtml, or not">
</form>
</div>
</body>
</html>[/code]


You have ruined the function with your other definitions of idArr in your recent post.
var idArr = Array();

idArr = {FAULTS_ARRAY};[/quote]


I don't know why you have those in there, but I had taken them out and used idArr as name of parameter for array of id's passed into function. If you need those other idArr definitions of yours for some reason which is not seen here, then you will just need to rename the things that I have defined as idArr in my example.
Copy linkTweet thisAlerts:
@paulnicauthorJan 09.2009 — The following works (no different from what I previously posted):

[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
<script type="text/javascript">

function listFaults(idPrefix,idArr) {
var listHtml = "", i = 0, idStr;
for (i = 0; i < idArr.length; i++) {
idStr = idPrefix + idArr[i];
if(document.getElementById(idStr).checked == true) {
listHtml += '<li>';
listHtml += 'html element with id of "' + idStr + '" has been checked!';
listHtml += '</li>';
}
}
return listHtml;
}

</script>
</head>
<body>
<div>
<form action="">
<input id="myIdPrefix_hasThisAddedToItInTheFunction" type="checkbox">
<input id="myIdPrefix_alsoHasThisAddedToItTemporarily" type="checkbox">
<input id="myIdPrefix_inTheForLoop" type="checkbox"><br>
<input type="button" onclick="alert(listFaults('myIdPrefix_',['hasThisAddedToItInTheFunction', 'alsoHasThisAddedToItTemporarily', 'inTheForLoop']));" value="See listHtml, or not">
</form>
</div>
</body>
</html>[/code]


You have ruined the function with your other definitions of idArr in your recent post.


I don't know why you have those in there, but I had taken them out and used idArr as name of parameter for array of id's passed into function. If you need those other idArr definitions of yours for some reason which is not seen here, then you will just need to rename the things that I have defined as idArr in my example.[/QUOTE]



thanks alot astupidname , please excuse my ignorance here though i am a javascript novice... where are you populating the array - idArr? i am passing in the array from php (hence idArr= {FAULTS_ARRAY}) but i cannot see where you are getting the values to put into the array?

Thanks

Paul
Copy linkTweet thisAlerts:
@astupidnameJan 09.2009 — where are you populating the array - idArr? i am passing in the array from php[/QUOTE]
O.k. that explains something. In your function in your original post:

function listFaults() {

listHtml = "";

var faultsArray = Array();

faultsArray = {FAULTS_ARRAY};


//think need some sort of loop here???

if(document.getElementById("Repaired"+insert id value+).checked == true) {

listString = "checked";

listHtml += "<li>";

listHtml += "some text to say which box is checked";

listHtml += "</li>";

}

return listHtml;

}[/quote]


You had not clarified where you were getting FAULTS_ARRAY from or that it is coming from php, or that it somehow contains the id's you are wanting to use? (you perhaps need to clarify that in particular). These are important details that you need to let us know, as well as letting us know what the FAULTS_ARRAY looks like exactly. It is hard to give accurate help otherwise, although my example is good as an example, but you need to give more info to enable us to help with the actual implementation if wanted.

In my example I am passing the id's into the function through parameters in the function call from the button.

i am passing in the array from php (hence idArr= {FAULTS_ARRAY})[/quote]

You had not shown any php usage in your post's.

So in your quote there, you're using idArr as an object actually {} and not actually an array? Tell me what FAULTS_ARRAY looks like in the php.

As an example, you should have been posting something more like this:

<?php $FAULTS_ARRAY = 'php variable code in here is what I want to see'; ?>

var idArr = { <?php echo $FAULTS_ARRAY ?> };

So you really need to 'come clean' perhaps and divulge more info, as now you are confusing me! (not hard to do!)
Copy linkTweet thisAlerts:
@paulnicauthorJan 09.2009 — ok sorry...

this is my php:

[COLOR="DarkRed"]$faultArrays = array();



foreach($list as $item)
{
if ($item['Repaired'] == 0){

$faultArrays[] = $item['Id'];


}
}[/COLOR]


i then pass this php array to my template file where my HTML and javascript is using a placeholder:

[COLOR="darkred"]$form->set('FAULTS_ARRAY',$faultsArray);[/COLOR]

so now i have my array of fault ids e.g (100,101,102,103)

i want to cycle through those within my javascript function and alert the user to which checkbox has been checked.(each checkbox has the unique id -repaired+Id value e.g Repaired100, Repaired101 etc.

hope this makes sense now!

Paul
×

Success!

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