Solved

'return' statement outside the function

Posted on 2008-06-24
34
1,093 Views
Last Modified: 2008-06-25
what does ot means and how to fix it?
<script language="JavaScript">

function get_check_value()

{

var c_value = "";

for (var i=0; i < document.form.ckbox.length; i++)

   {

   if (document.form.ckbox[i].checked)

      {

      c_value = c_value + document.form.ckbox[i].value + "\n";

      }

   }

}

</script>

..

<%

response.write "<input type=""checkbox"" name=""ingrid"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"

%>

..

<A href ="javascript:return get_check_value();if return getcustomer(return)";>find values</A>

Open in new window

0
Comment
Question by:Forrest_Gump
  • 23
  • 6
  • 5
34 Comments
 
LVL 14

Expert Comment

by:Tchuki
ID: 21862621
It is telling you that:

>>  <A href ="javascript:return get_check_value();if return getcustomer(return)";>find values</A>

Is incorrect.  Your return keyword should be inside your function, not as part of the call to the function as you have it.


<script language="JavaScript">
function get_check_value()
{
var c_value = "";
for (var i=0; i < document.form.ckbox.length; i++)
   {
   if (document.form.ckbox[i].checked)
      {
      c_value = c_value + document.form.ckbox[i].value + "\n";
      }
   }
return c_value;
}
</script>

<A href ="get_check_value();">find values</A>
0
 

Author Comment

by:Forrest_Gump
ID: 21862639
dear tchuki,
can you please help me with that:(
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23513072.html

it is connected to this question...from your answer here i understand that the solution i got there is wrong:(
0
 

Author Comment

by:Forrest_Gump
ID: 21862646
<A href ="get_check_value();">find values</A>
that is doing only part of the task...
i need to take all what it returns and to send to another function called getcustomer
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 21862669
Try:

<script language="JavaScript">
function get_check_value()
{
      var c_value = "";

      for (var i=0; i < document.form.ckbox.length; i++)
      {
            if (document.form.ckbox[i].checked)
            {
                  c_value = c_value + document.form.ckbox[i].value + "\n";
            }
      }

      if ( c_value != "" )
      {
            getcustomer ( c_value );
      }

}

function getcustomer ( customer )
{
      // do something with the customer
      // then call return
}
</script>

<A href ="get_check_value();">find values</A>
0
 

Author Comment

by:Forrest_Gump
ID: 21862676
this structure will be good?
response.write "<input type=""checkbox"" name=""ingrid"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"
0
 

Author Comment

by:Forrest_Gump
ID: 21862690
when click on <A href ="get_check_value();">find values</A>
it just going to the same url
http://www.domain.com/get_check_value();
0
 

Author Comment

by:Forrest_Gump
ID: 21862700
when i try this:
<A href ="JAVASCRIPT:get_check_value();">find values</A>

i get error:
document.form.ckbox is null ornot an object
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 21862701
Sorry, I overlooked that bit.  Was concentrating on the return error rather than checking for other errors.

<A href="#" onclick="get_check_value();">find values</A>

0
 

Author Comment

by:Forrest_Gump
ID: 21862711
with

<A href="#" onclick="get_check_value();">find values</A>

i still got
i get error:
document.form.ckbox is null ornot an object
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 21862721
Paste your form code in here.
0
 

Author Comment

by:Forrest_Gump
ID: 21862735
i got no form tags...i have only those:

      <p><input type="CHECKBOX" name="menu" value="1"> This...</p>
      <p><input type="CHECKBOX" name="menu" value="2"> That...</p>
      <p><input type="CHECKBOX" name="menu" value="3"> ...and The Other</p>

<A href="#" onclick="get_check_value();">find values</A>

and of course above that the connection to the js
<script src="selectcustomer.js"></script>

do i need a form?
if i do please tell me the syntax of the first line-because i am not sure how to fit it to the script
<form name=ckbox>?
..
0
 

Author Comment

by:Forrest_Gump
ID: 21862738
the checkboxes being generated with
response.write "<p><input type=""checkbox"" name=""menu"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "</p>"
0
 
LVL 14

Expert Comment

by:Tchuki
ID: 21862756
>>  document.form.ckbox

Is looking for a checkbox in a form.

<form action="" method="">
response.write "<p><input type=""checkbox"" name=""menu"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "</p>"
</form>
0
 

Author Comment

by:Forrest_Gump
ID: 21862769
<form action="" method="">
...
response.write "<p><input type=""checkbox"" name=""menu"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "</p>"
...
</form>

still document.form.ckbox is null ornot an object:(
0
 

Author Comment

by:Forrest_Gump
ID: 21862777
the <A href="#" onclick="get_check_value();">find values</A> got to be inside the form?
0
 

Author Comment

by:Forrest_Gump
ID: 21862780
nope...even when  the <a href is inside i got
document.form.ckbox is null ornot an object
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21862938
1. the name of the checkbox is here ingrid and not ckbox
2. it is IMPERATIVE that you return false on onClick

<a href="#"
onClick="something(); return false">

that can be done by returning false in the function

function somethingCalledFromHref() {
.
.
.
  return false
}

and then use this construct



<a href="#"
onClick="return somethingCalledFromHref()">


In your case you most likely want



<script language="JavaScript">

function get_check_value() {

  var c_value = "";

  for (var i=0; i < document.form.ingrid.length; i++) {

    if (document.form.ingrid[i].checked) {

      c_value = c_value + document.form.ingrid[i].value + "\n";

    }

  }

  return c_value; // may be empty

}

 

function getcustomer(customers) {

  if (customers=="") return; // leave the function

  // process customers

 

}

</script>

..

<%

response.write "<input type=""checkbox"" name=""ingrid"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"

%>

..

<A href="#" onClick="getcustomer(get_check_value()); return false">find values</A>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21862951
perhaps you want to keep the name ckbox and not loose the first name information "ingrid " :

"<input type=""checkbox"" id=""ingrid"" name=""ckbox"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"

Now you can change the script back to using ckbox and if you need, get the ingrid as document.form.ckbox[i].id
0
 

Author Comment

by:Forrest_Gump
ID: 21863171
dear mplungjan,thank you for you explanation...now i understand more but notas much to solve my enigma.
please look into here
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23513072.html

the aim of my problem was nt solved because finnaly i need to send to the function showcustomer(not getcustomer) all the values of the checked boxes.
0
 

Author Comment

by:Forrest_Gump
ID: 21863181
that is the function showCustomer:
var xmlHttpSel1

 

function showCustomer(str)

{ 

xmlHttpSel1=GetXmlHttpObject();

if (xmlHttpSel1==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  } 

var url="getcustomer.asp";

url=url+"?menu="+str;

url=url+"&sid="+Math.random();

xmlHttpSel1.onreadystatechange=stateChanged;

xmlHttpSel1.open("GET",url,true);

xmlHttpSel1.send(null);

}

 

function stateChanged() 

{ 

if (xmlHttpSel1.readyState==4)

{ 

document.getElementById("txtHint").innerHTML=xmlHttpSel1.responseText;

}

}

Open in new window

0
 

Author Comment

by:Forrest_Gump
ID: 21863294
if you reading this please not look at my comments above.

here is what i did, that gives me the old error:
document.form.ingrid is null ornot an object
selectcustomer.js:

-------------------------

function get_check_value() {

  var c_value = "";

  for (var i=0; i < document.form.ingrid.length; i++) {

    if (document.form.ingrid[i].checked) {

      c_value = c_value + document.form.ingrid[i].value + "\n";

    }

  }

  return c_value; // may be empty

}

 

function showingr(customers) {

  if (customers=="") return; // leave the function
 

{ 

xmlHttpSel2=GetXmlHttpObject();

if (xmlHttpSel2==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  } 

var url="getcustomer.asp";

url=url+"?ingr="+str;

url=url+"&sid="+Math.random();

xmlHttpSel2.onreadystatechange=ingrChanged;

xmlHttpSel2.open("GET",url,true);

xmlHttpSel2.send(null);

}

 

function ingrChanged() 

{ 

if (xmlHttpSel2.readyState==4)

{ 

document.getElementById("txtHint").value=xmlHttpSel2.responseText;

}

}
 

 

}
 

and in the file getcustomer.asp:
 
 

<form>

<%

...

response.write "<input type=""checkbox"" name=""ingrid"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"

...

%>
 

<A href="#" onClick="getcustomer(get_check_value()); return false">find values</A>

</form>

Open in new window

0
 

Author Comment

by:Forrest_Gump
ID: 21863310
i mean
<A href="#" onClick="showingr(get_check_value()); return false">find values</A>
0
 

Author Comment

by:Forrest_Gump
ID: 21863323
and in the ingrid function i changed to
url=url+"?ingr="+customers;
0
 

Author Comment

by:Forrest_Gump
ID: 21863327
document.form.ingrid is null ornot an object:(
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21863351
One last time:


function get_check_value() {
  var c_value = "";
  var theChecks = document.forms[0].menu; // assuming the first form [0] on the page
  for (var i=0; i < theChecks.length; i++) {
    if (theChecks[i].checked) {
      c_value += ','+theChecks[i].value;
    }
  }
  return (c_value)? c_value.substring(1):"" // remove leading comma
}
.
.


<%
response.write "<input type=""checkbox"" name=""menu"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"
%>
..
<A href="#" onClick="showCustomer(get_check_value()); return false">find values</A>
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 21863381
Or now I see you managed to comment a lot:
function get_check_value() {

  var c_value = "";

  var theChecks = document.forms[0].ingrid; // assuming the first form [0] on the page

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

    if (theChecks[i].checked) {

      c_value += ','+theChecks[i].value;

    }

  }

  return (c_value)? c_value.substring(1):"" // remove leading comma

}

.

.

 

<FORM>

<%

response.write "<input type=""checkbox"" name=""ingrid"" value='" & rstemp1("id") & "' >" & rstemp1("subcat") & "<br>"

%>

..

 

</FORM>

<A href="#" onClick="showCustomer(get_check_value()); return false">find values</A>

Open in new window

0
 

Author Comment

by:Forrest_Gump
ID: 21863384
you changed the all function structure
FROM:
function get_check_value() {
  var c_value = "";
  for (var i=0; i < document.form.ingrid.length; i++) {
    if (document.form.ingrid[i].checked) {
      c_value = c_value + document.form.ingrid[i].value + "\n";
    }
  }
  return c_value; // may be empty
}

TO:

function get_check_value() {
  var c_value = "";
  var theChecks = document.forms[0].ingrid; // assuming the first form [0] on the page
  for (var i=0; i < theChecks.length; i++) {
    if (document.theChecks[i].checked) {
      c_value += ','+theChecks[i].value;
    }
  }
  return (c_value)? c_value.substring(1):"" // remove leading comma
}


?
0
 

Author Comment

by:Forrest_Gump
ID: 21863407
i am sorry for the 3Q i wil close them and lets talk here.

now when o changed to function i have error:
document.theChecks is null or not an object
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21863413
Yes, since I do not believe your server wants a carriage return in the string so I changed it to commas like you would have if you submitted the form with the checkboxes normally

document.form is also only correct if you have
<form name="form"

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 21863416
Sorry, typo/cut and paste error
change
if (document.theChecks[i].checked) {
to
if (theChecks[i].checked) {
0
 

Author Comment

by:Forrest_Gump
ID: 21863435
i got other forms on the page
if i will give to the form and id="ingr"
how i need to change
 var theChecks = document.forms[0].ingrid; // assuming the first form [0] on the page
?
0
 

Author Comment

by:Forrest_Gump
ID: 21863490
http://ilmc.brinkster.net/cpp/web/getcustomer.asp
that is not doing anything:(
i got no error but i am clicking on the
      <p><input type="CHECKBOX" name="ingrid" value="3"> ...and The Other</p>
and that is not going anywhere...
0
 

Author Comment

by:Forrest_Gump
ID: 21863504

function get_check_value() {

  var c_value = "";

  var theChecks = document.forms[0].ingrid; // assuming the first form [0] on the page

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

    if (theChecks[i].checked) {

      c_value += ','+theChecks[i].value;

    }

  }

  return (c_value)? c_value.substring(1):"" // remove leading comma

}
 
 

function showingr(customers) {

  if (customers=="") return; // leave the function
 

{ 

xmlHttpSel2=GetXmlHttpObject();

if (xmlHttpSel2==null)

  {

  alert ("Your browser does not support AJAX!");

  return;

  } 

var url="getcustomer.asp";

url=url+"?ingr="+customers;

url=url+"&sid="+Math.random();

xmlHttpSel2.onreadystatechange=ingrChanged;

xmlHttpSel2.open("GET",url,true);

xmlHttpSel2.send(null);

}

 

function ingrChanged() 

{ 

if (xmlHttpSel2.readyState==4)

{ 

document.getElementById("txtHint").value=xmlHttpSel2.responseText;

}

}
 

 

}

Open in new window

0
 

Author Comment

by:Forrest_Gump
ID: 21863797
dear mplungjan,
you soved the original question problem so i am closing this question and thakyou for that.
but the origianl problem was not solved so please help me here:
http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_23513718.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now