Reputation: 4061
I have:
<textarea class="t1" onchange="function1('1','2','3')"></textarea>
<script type="text/javascript">
$(function() {
var function1 = function(p1, p2, p3) {
alert('p1:' + p1 + ' p2:' + p2 + ' p3:' + p3);
};
});
</script>
However when I change the text of the textarea, nothing happens.
I get this error on the console:
Uncaught ReferenceError: function1 is not defined
Upvotes: 1
Views: 687
Reputation: 80649
domReady
declaration.Currently, your function is parsed like this:
$(window).load(function(){
var function1 = function (p1, p2, p3) {
alert('p1:' + p1 + ' p2:' + p2 + ' p3:' + p3);
};
});
You need it inside the head
tag instead. Chose no wrap - in <head>
option from dropdown.
Upvotes: 1
Reputation: 10219
If you're using jQuery :
<textarea class="t1"></textarea>
<script type="text/javascript">
$(function() {
$('.t1').on('change', function(){
function1('1','2','3');
});
var function1 = function(p1, p2, p3) {
alert('p1:' + p1 + ' p2:' + p2 + ' p3:' + p3);
};
});
</script>
That makes me learn that the event change
on a textarea is like blur
for jQuery… Not sure, that's what you want in the end.
EDIT after comment
<textarea class="t1" data-pam1="1" data-pam2="2" data-pam3="3"></textarea>
<script type="text/javascript">
$(function() {
var function1 = function(p1, p2, p3) {
alert('p1:' + p1 + ' p2:' + p2 + ' p3:' + p3);
};
$('.t1').on('change', function(){
var param1 = $(this).data('pam1'),
param2 = $(this).data('pam2'),
param3 = $(this).data('pam3');
function1(param1,param2,param3);
});
});
</script>
Upvotes: 1
Reputation: 1889
That is because your function1 is only in the scope of the anonymous function passed to jQuery.
As you're using jQuery, why not use it fully? Attach a .change()
handler to .t1
-
$(function () {
$(".t1").change(function(){
alert("Do your thing here");
});
});
Upvotes: 0
Reputation: 28753
Try like this
<script type="text/javascript">
var function1 = function(p1, p2, p3) {
alert('p1:' + p1 + ' p2:' + p2 + ' p3:' + p3);
};
</script>
<textarea class="t1" onchange="function1('1','2','3')"></textarea>
Nothing first you need to define(initialize) the function and then you can call it....and dont define it under
$(function(){..HERE..})
then it comes under GLOBAL scope
Upvotes: 1