javascript - Can 'return' be used to break a JS function? -


i beginner , trying write scratch: want 'write' inputs text fields textarea. made following (also viewable on http://jsfiddle.net/jolarti/fx6xl/1/ )

html:

<input type='text' id='fruit' value='fruit'> <br/> <input type='text' id='salad' value='salad'> <p>this meal made:</p> <textarea id="mytext" rows="4" cols="50"></textarea> <br /> <button>make food</button> 

script:

$(document).ready(function () {      $("#fruit");     $("#salad");     $("button").click(function () {         var fruit = $("#fruit").val();         var salad = $("#salad").val();          if (fruit === "" || salad === "") {             alert("type in both ingredients in order make something!!");         } else {             //alert("would " + fruit + " " + salad + " have lunch?");             var phrase = ("would " + fruit + " " + salad + " have lunch?");             document.getelementbyid("mytext").innerhtml = phrase; //writes textarea             return false; //i use end script , not make page disappear         }     });  }); 

am using 'return' right purpose here? works , want but... practice or bad practice? put 'return' in, because if don't, page becomes empty after input! + need explanation on why happens. , wonder: use 'return' write output screen?

yes, work, try not typing , see doesn't reach return false; submits ;)

that said, try this:

<button type="button">make food</button> 

now don't need return false; because button button.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -