Jump to content

Javascript Arrays - need help

- - - - -

  • Please log in to reply
No replies to this topic

#1
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hi there,

Javascript is not really my thing but sometimes I do need to use it in my scripts to make it work more dynamicly but then fast I run into trouble with trying to think up the way it should be scripted in javascript myself :closedeyes:

So here's the situation: I'd like to have an array in javascript which contains all target ( selected ) divs by the user, so the user can select divs easily and remove them ( => add/remove the target id from the targets array). But the script won't work as I dunno how to do such things in javascript.. Here's what I got though:

var target_id = []; //the target array that should contain all target selected divs
 
//function to check if a target div is in the array ( not sure if this works )
function in_array (needle, haystack, argStrict) {
var key = '', strict = !!argStrict; 
if (strict) for (key in haystack) if ( (strict && haystack[key] === needle) || ( !strict && strict && haystack[key] == needle ) ) return true; return false; 
} 
 
//function to remove a target div from the array
function removeItem(array, item) { 
var i = 0;
while (i < array.length) {
if (array[i] == item) {
array.splice(i, 1);
} else {
i++;
}
}
return array;
}
 
function setTarget(fileid) {
xmlhttp2=GetXmlHttpObject();
if (xmlhttp2==null)
{
alert ("Your browser does not support AJAX!");
return;
}
 
if(in_array(fileid, target_id)) {
 
//target div already selected ( already in array of selected/target divs? )
var obj = document.getElementById(fileid);
obj.style.backgroundColor = "";  //remove the target background-color back to normal, white
var removeTarget = removeItem(target_id, fileid); //remove the target div from the selected divs in array
var url="updateTarget.php?removeTarget=";
url=url+fileid;
xmlhttp2.onreadystatechange=stateChanged2;
xmlhttp2.open("GET",url,true);
xmlhttp2.send(null);
 
}else{
 
//target div not selected yet / not in array of selected target divs yet -> add it 
var obj = document.getElementById(fileid);
obj.style.backgroundColor = "BABABA"; //mark the selected div lightgray
var target_arr_len = target_id.length;
target_id[target_arr_len] = fileid; //add the new target div to the array of target selected divs
var url="updateTarget.php?setTarget=";
url=url+fileid;
xmlhttp2.onreadystatechange=stateChanged2;
xmlhttp2.open("GET",url,true);
xmlhttp2.send(null);
 
}
 
}

The AJAX part is just to call the PHP script which sets the path as well into a session variable for the server sided use of it.

Any suggestions how to get this to work?

Thanks in advance again!

Cheers.

EDIT: Fixed, reckon to just use the values as index keys of the array, makes it much simpler, especially as that's the most important value to work with and easier to track when used as key ($arr[key])

And sorry for posting in wrong section!

Edited by webcodez, 15 April 2010 - 02:41 AM.
Posted in wrong section





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users