Well, I was having a similar problem in another script and I fixed it by switching the single quotes for double quotes within the index definition.
so myArray["this"] instead of myArray['this'].
But here it didn't work at all.
Printable View
Well, I was having a similar problem in another script and I fixed it by switching the single quotes for double quotes within the index definition.
so myArray["this"] instead of myArray['this'].
But here it didn't work at all.
Technically, in JavaScript there is no difference between single and double quotes as delimiter, except the case when there are the same type of quotes inside the string to be treated literally. If so, the inner quotes must be escaped.
It is not the same as in PHP, so I don't see how you could fix a problem by switching the quotes.Code:var myString="It's my life";
// or
var myString='It\'s my life';
Okay. By commenting out every bit of my javascript except this array and the function that uses it, I have deduced that the problem is in fact stemming from somewhere else in the code...
*rubs his face* is there a good way to debug javascript? In PHP if you have some sort of syntax error or what not, it tells you what and where. This is my first real experience with Javascript, so is there something like that for this?
You probably have written the JavaScript code on using a PHP echo. Now, the quotes problem was not a JavaScript problem, but a PHP one, thus you could have solved using the escape as well.
But, really, I am confused now. Does your code works, or not? Are we talking about JavaScript or PHP?
Let me back up.
I'm using PHP to parse MySQL data into JavaScrip arrays that I use to control data as the end user fills out a form.
I'm using the array method as opposed to the object method for 2 reasons: 1) it is a simpler method to generate using PHP for loops, and 2) when I write "alert(myArray);" it traces all the values held in myArray, where if I write "alert(myObject);" it traces "[object Object]".
After much work, I deduced that the problem I was facing was actually stemming from a completely separate function that was sitting quite a few lines up from the one I was working on. So at this time I'm just trying to figure out what the problem is so I can fix it.
Additionally, I'm wondering if JavaScript has a debug feature similar to PHP where an error will be printed to the screen with information regarding the type and location of the error.
Okay now. I've tracked the error to a single line and determined the malfunction.
I have a function that analyzes the selection from a select box and uses that data to set the display of the appropriate div element for the end user to supply additional information if necessary.
I had a need to execute this function on load in case POST data contained a selected value that would require additional fields to be made visible before the end user supplied any input. The problem was that if this function were executed with an empty string (or any invalid data) then it would cause an error when the function passed that string to the function that sets the visibility of the div element.
Basically, since that data is used to assemble the appropriate id, if invalid data was passed then it would target a div element that doesn't exist; and that is what was causing my error...
What I had to do to find it was to eliminate all interference from other scripts and processes by commenting them out; there was no fault in my array nor function at all.
Moral: some scripts don't play well with others. sometimes you gotta put em in time out to sort out who did what.
1. Well, there is no reason for PHP should use more code lines to generate a JavaScript Object than a JavaScript Array:
2. That is normal. on alert() an array display its elements. But in practice you don't need the alert, you need the values. To circle through the elements of an array, use an ordered for loop for(var i=0;i<array.length;i++). To circle through the pairs property:value of an object use an unordered loop for(var p in object) (where the variable p is implicit - you may name it whichever - it will automatically get the value of each property in a loop)Code:<script type="text/javascript">
//creates an array
var array=[];
<?php
$myArray=array('value1','value2','value3');
$i=0;
foreach($myArray as $key){
echo "array[".$i."]='".$key."';";
$i++;
}
?>
var arrString='';
for(var i=0;i<array.length;i++){
arrString+=(i+' : '+array[i]+' | ');
}
alert(arrString);
//creates an object
var object={};
<?php
$myObject=array('value1'=>'property1','value2'=>'property2','value3'=>'property3');
foreach($myObject as $key=>$value){
echo "object['".$myObject[$key]."']='".$key."';";
}
?>
var objString='';
for(var p in object){
objString+=(p+' : '+object[p]+' | ');
}
alert(objString);
</script>