function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sinsiterrulezsinsiterrulez 

Please Help!!!..Javascript for loop

Hi, I have a Javascript function onload which has a for loop..But the for loop is not working.Its throwing some absurd error. Please help

Below is the code & the error:

 

Code:

window.onload = function()
{
    if(("{!$CurrentPage.parameters.id}").length > 0)
    {
        alert('Im in begin function');
        var Sprod = ("{!PSS.AT_Product__c}").split(';');
        alert(Sprod.length);
        var mainElem=document.getElementById(Hidden);
        var sElem=document.getElementById("Productd_selected");
        var uElem=document.getElementById("Productd_unselected");
        var sI=0;
        var uI=0;
        sElem.length=0;
        uElem.length=0;
        alert(mainElem.length);
        for (i=0;i<Sprod.length;i++)
        {
            mainElem.options[parseInt(Sprod[i])].selected=true;
        }   
     }      
}

 

 

The error which I 'm getting is:

Uncaught SyntaxError:Unexpected identifier 

 

On viewing the page source what I get is:

window.onload = function()
 {
  if(("*******************").length> 0)
  {
  alert('Im in begin function');
  var Sprod = ("abc;cde;def").split(';');
  alert(Sprod.length);
  var mainElem=document.getElementById(Hidden);
  var sElem=document.getElementById("Productd_selected");
  var uElem=document.getElementById("Productd_unselected");
  var sI=0;
  var uI=0;
  sElem.length=0;
  uElem.length=0;
  alert(mainElem.length);
  insert();
  for (i=0;i<Sprod label="Chosen" .length;i++)
  {
  mainElem.options[parseInt(Sprod[i])].selected=true;
  }
  }
 }
Can anyone help me.I have tried everything..Please HELP!!!!!!!!!!!

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

HI

After much of research what I found out was that if we have for loop in javascript in VF pages then the condition should have spaces between it i.e.

for(var i=0; i< TableRows display="inline" .length; i++){

 Its because if we dont have a space in condition i.e. i<TAbleRows then the interpreter assumes it as a VF tag.

This is a bug in VF..

so always add spaces between conditions.

 

All Answers

bob_buzzardbob_buzzard

Can you repost your code using the Insert Code link (clipboard icon with a little C in it), as some of your code is turning into smileys.

 

 

sinsiterrulezsinsiterrulez

IGNORE THIS POST!!

Message Edited by sinsiterrulez on 03-08-2010 05:47 AM
sinsiterrulezsinsiterrulez

The Code:

 

window.onload = function(){

if(("{!$CurrentPage.parameters.id}".length > 0){

if(("{!$CurrentPage.parameters.id}".length > 0){

alert('Im in begin function');

var Sprod = ("{!PSS.AT_Product__c}".split(';');

alert(Sprod.length);

var mainElem=document.getElementById(Hidden);

var sElem=document.getElementById("Productd_selected");

var uElem=document.getElementById("Productd_unselected");

var sI=0;

var uI=0;

sElem.length=0;

uElem.length=0;

alert(mainElem.length);

for (i=0;i<Sprod.length;i++){

mainElem.options[parseInt(Sprod[i])].selected=true;

}

}

}

 

 and the error:

 

window.onload = function(){

if(("oo6abcdefghi123".length> 0){

alert('Im in begin function');

var Sprod = ("abc;cde;def").split(';');

alert(Sprod.length);

var mainElem=document.getElementById(Hidden);

var sElem=document.getElementById("Productd_selected");

var uElem=document.getElementById("Productd_unselected");

var sI=0;

var uI=0;

sElem.length=0;

uElem.length=0;

alert(mainElem.length);

for (i=0;i<Sprod label="Chosen" .length;i++){

mainElem.options[parseInt(Sprod[i])].selected=true;

}

}

}

 

 

 

 

bob_buzzardbob_buzzard

The brackets don't match on the first two if statements (although looking at your code maybe its only one?) - both open two round brackets but only close one.

 

I realise this isn't the issue that you have highlighted, but often with javascript you have to move in baby steps. 

sinsiterrulezsinsiterrulez

I closed the bracket but the error is still not resolved.. Do you any idea why am I getting the label="chosen" in for loop condition even when I havent mentioned it ?

Its the first time I'm facing such issue..

I tried using a do..while statement & to my surprise it worked but if I implement the same using for loop I get an error with page source showing that label thing in the code

bob_buzzardbob_buzzard

No idea at all why that would be.

 

That said, I haven't seen the entire page and controller, so its possible there's something else causing that to happen.

Craig_XactiumCraig_Xactium

Sorry to bring up an old post, but is there any fix for this?

 

I have a javascript for loop and salesforce is randomly moving around my code causing it to error.

 

Salesforce keeps on rendering this code:

 

 

for(var i=0; i<TableRows.length; i++){
	TableRows[i].style.display = 'inline';
}	

 

as this:

 

for(var i=0; i<TableRows display="inline" .length; i++){
TableRows[i].style.;
}

Which would appear to be the same issue as the original poster

 

sinsiterrulezsinsiterrulez

HI

After much of research what I found out was that if we have for loop in javascript in VF pages then the condition should have spaces between it i.e.

for(var i=0; i< TableRows display="inline" .length; i++){

 Its because if we dont have a space in condition i.e. i<TAbleRows then the interpreter assumes it as a VF tag.

This is a bug in VF..

so always add spaces between conditions.

 

This was selected as the best answer