0

So I have code like this:

<script language="javascript">
$(function(){
    function hellothere(strng){
        showalert(strng);
    }
    function showalert(showit){
        alert(showit);
    }
});
</script>
<input type="button" onclick="hellothere('hi');" value="jquery"/>

When I click on the button though, it throws a javascript error (Object expected). I have also tried the following but they also throw javascript errors (Object does not support this property).

<input type="button" onclick="$.hellothere('hi');" value="jquery"/>

and

<input type="button" onclick="$.fn.hellothere('hi');" value="jquery"/>

What am I missing?

UPDATE: This is what's happening, to clarify alittle. I have a button:

<input type="button" class="mybutton" value="jquery"/>

On page load, I assign an onclick event to the button with the $():

$(function(){
     $(".mybutton").click(function(){
     //Do some stuff
     mybuttoncallback(dataprocessed);

     });

     function senditoff(finaldata){
          //send the data off
     }
});

So this button is on a couple pages but is does different things with the data that's processed. So the 'mybuttoncallback' is a javascript method that is local to the page itself since different pages may handle the data differently.

function mybuttoncallback(thedata){
    //process the data
    senditoff(hereitis);
}

The 'senditoff' method is in the jquery code since all the pages send it off the same way. I would prefer not to move anything out of the $() because many pages would have to change. (The 'senditoff' method is the 'hellothere' method from the previous example.)

So have can the javascript method call the jquery 'senditoff' method??

3 Answers 3

2

If you really need to extend the jQuery object, try this:

<script language="javascript">
$.hellothere = function(strng){ alert(strng); }
</script>
<input type="button" onclick="$.hellothere('hi');" value="jquery"/>
1
  • BRILLIANT! I don't have to take it out of the $() though. This way, it can work with methods in $() and still be called from outside the $(). I have modified your answer to reflect this. Thanks!
    – dev4life
    Apr 26, 2011 at 22:14
0

Try this. You don't need any special attribute of jquery to do this.

<script language="javascript">        
     function hellothere(strng){
        alert(strng);
     }       
</script>
<input type="button" onclick="hellothere('hi');" value="jquery"/>
1
  • I would prefer not to move it out because the 'hellothere' calls other methods within the jquery initialization.
    – dev4life
    Apr 26, 2011 at 21:49
0

move this function out of document.ready to make it global

You are getting the error because of scope issues.

  function hellothere(strng){
                alert(strng);
            }
1
  • I would prefer not to move it out because the 'hellothere' calls other methods within the jquery initialization.
    – dev4life
    Apr 26, 2011 at 22:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.