6

Gives an error. I have placed the code just before </body>. Still getting the error.

<form action="" method="get" id="searchform" >
<input name="q" type="text" id="search" size="32" maxlength="128" class="txt">
<input type="button" id="hit" value="Search" onclick="myFunction();return false" class="btn"> 
</form>

JS,

<script type="text/javascript">
 var nexturl = "";
 var lastid = "";
 var param;
 $(document).ready(function () {
 function myFunction() {
 param = $('#search').val();
 alert("I am an alert box!");
 if (param != "") {
 $("#status").show();
 var u = 'https://graph.facebook.com/search/?callback=&limit=100&q=' + param;
 getResults(u);
 }
 }
 $("#more").click(function () {
 $("#status").show();
 $("#more").hide();
 pageTracker._trackPageview('/?q=/more');
 var u = nexturl;
 getResults(u);
 });
 });
</script>
Derek 朕會功夫
94.8k45 gold badges199 silver badges255 bronze badges
asked Mar 2, 2013 at 5:57
2
  • 1
    Just get rid of the $(document).ready() construct. It's not needed when you have your code at the bottom. Commented Mar 2, 2013 at 6:06
  • Bind the event handler with jQuery instead of using onclick. Commented Mar 2, 2013 at 6:10

4 Answers 4

15

You cannot place myFunction after the onclick. When the onclick is seen there is no definition for myFunction.

Place the JavaScript in <head> tag. Also, move the function outside of ready().

Like this:

<script type="text/javascript">
var nexturl ="";
var lastid ="";
var param;
function myFunction() {
 param = $('#search').val();
 alert("I am an alert box!");
 if (param != "") {
 $("#status").show();
 var u = 'https://graph.facebook.com/search/?callback=&limit=100&q='+param;
 getResults(u);
 }
}
$(document).ready(function() {
 $("#more").click(function () { 
 $("#status").show();
 $("#more").hide(); 
 pageTracker._trackPageview('/?q=/more');
 var u = nexturl;
 getResults(u);
 });
});
</script>
</head>
<body>
...
answered Mar 2, 2013 at 6:00
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't matter if myFunction is before or after the onclick attribute. It only matters that it is hidden in the variable scope of the .ready() handler..
@ATOzTOA: Moved the code outside but still it doesn't seem to work.
Moved the function outside the ready block and moved the complete code in the head and it worked.
5

keep myFunction in script tag directly

 i.e
 <script>
function myFunction() {
 ..... 
}
 </script>
answered Mar 2, 2013 at 6:00

1 Comment

please mark it as answer if you got the solution with this answer :)
1

From the jQuery docs:

The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.

So your function isn't created until after your onclick is established. Thus it can't find the function. You'll want to move it outside the $(document).ready(function(){}).

Derek 朕會功夫
94.8k45 gold badges199 silver badges255 bronze badges
answered Mar 2, 2013 at 6:02

5 Comments

@FahadUddin What happens the second time? Are you seeing an error?
The function runs again but does not perform the task it perfomrms at first
@FahadUddin It's working for me; however, the returned results are being appended to the current status list. Maybe they should replace it?
Thanks. I just observed that.
Just add $('#data').empty() to the top of the success function, if you don't want the previous results to persist. And if you found my answer helpful, feel free to mark it as your answer.
0

You have to move the function outside the $document.ready here then you will have two options: 1st move it to <head> or 2nd move it right before the closing $document.ready bracket. Use this type of declaration for your function myFunction(){alert("inside my function");};

answered Mar 21, 2018 at 23:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.