?
Solved

Need clarification on behavior of script

Posted on 2005-04-29
5
Medium Priority
?
202 Views
Last Modified: 2006-11-18
<form method = "post" onsubmit = "return vf(this)">   <-- confusion here
           <input type = "text" id = "t" />
           <input type = "submit" value = "submit" />
</form>
<script>
           function vf(frm) {
                      return document.getElementById("t").value.length > 0;
           }
</script>

what exactly does the onsubmit = "return vf(this)" do?  I know the function vf will return a boolean but why does onsubmit have a "return" as well?  And what happens when this is true/false?
0
Comment
Question by:CBMLude
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:sajuks
ID: 13899766
if u dont put the return then the page will get validate, post erros if any and proceed to the next page
try whithout the return the below script and then with return
<form method = "post" action = "http://www.google.com" onsubmit = "vf(this)">  
           <input type = "text" id = "t" />
           <input type = "submit" value = "submit" />
</form>
<script>
           function vf(frm) {
                      return document.getElementById("t").value.length > 0;
           }
</script>
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 80 total points
ID: 13899775
The HTML submit is looking for a true or false on the onsubmit event to determine if the form should be submitted or not.

If the onsubmit is false, the form will not be submitted.

return vf(this) in the onsubmit says to return the value of the function vf() to the HTML form handler.

If the form element t has a length greater than zero then the vf function retuns true.  If you did not have a return ... inside the vf function it would have no return value.

so, inside onsubmit="..." you have inline javascript, equivalent to a function.  It also has to have a return statement, or there will be no return value for the HTML handler to receive.

Sajuks, I know you answered, however there were some typos making it hard to understand your answer.  I was just trying to simplify.  You're welcome to the points.

Rod
0
 
LVL 33

Expert Comment

by:sajuks
ID: 13899793

an answer which is so well phrased deserves recognition...
" You're welcome to the points."
u can've it for a good explanation...'ve enough with me for this month atleast ;-)
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 13899877
LOL, just didn't want you to think I was stepping on your toes...only trying to help the question poster.

Glad you're not offended.

Rod
0
 

Expert Comment

by:gurujr
ID: 13901852
onsubmit allows you to execute a peice of code [function] , after the user have clicked "enter" or "submit" button on a form ,
then you can use this function to verify or modify data before it is sent to the server, if you return false the form wont be submitted if return true , the form will get submitted.

so you can run a function, for validating , adding/changing values to your inputs before your page change to the receving page.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

807 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