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"/>
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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 Comment

I would prefer not to move it out because the 'hellothere' calls other methods within the jquery initialization.
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 Comment

I would prefer not to move it out because the 'hellothere' calls other methods within the jquery initialization.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.