Reputation: 882
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??
Upvotes: 0
Views: 450
Reputation: 4180
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"/>
Upvotes: 2
Reputation: 15835
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);
}
Upvotes: 0
Reputation: 572
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"/>
Upvotes: 0