I'm trying to call a function in onchange given to a text box. Whenever I focusout of a text box, the function should be called. But I'm getting myFunction(function name) is undefined. This is what I have done:
var c = 0;
jQuery(document).ready(function() {
jQuery("#table_projection_value").dataTable({
"sAjaxSource": "includes/inc-projection-db.php?mode=projection_dataTable",
"bDestroy": true,
"bPaginate": false,
"bInfo": false,
"bFilter": false,
"bSort": false,
"aoColumnDefs": [
{
"aTargets": [0],
"mRender": function(data, type, row) {
return data + '<input type="hidden" class="user_id" name="user_id[]" id="user_id" value="' + row[4] + '">';
}
},
{
"aTargets": [1],
"mRender": function(data, type, row) {
return '<input type="text" onchange="myFunction();" class="form-control text-right projected_value" name="projected_value[]" id="projected_value_' + c + '_' + data + '" >';
}
}
],
"fnCreatedRow": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){
c = c + 1;
}
});
function myFunction(){
$(":text").blur(function() {
alert("**");
var element=$(this); // you can get the element here
});
$(":text").focusout(function() {
alert(this.id + " focus out");
});
}
});
What should I do?
asked Apr 6, 2016 at 5:04
Electronic Brat
1431 gold badge2 silver badges11 bronze badges
2 Answers 2
So the code within the jQuery function wont be directly accessable to you after the document is ready, you either move the function out of there or call your function in there:
jQuery(document).ready(function() {
myFunc() {
//code
}
// call here
})
or
jQuery(document).ready(function() {
//code
})
myFunc() {
//code
}
// call here
answered Apr 6, 2016 at 5:07
omarjmh
13.9k6 gold badges37 silver badges42 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Electronic Brat
Thanks dude, for your help. I got it. But now, I face another problem. I'm trying to get the value of that text box, where I get "TypeError: this.id.val is not a function" error. How to resolve this problem and get the value. This is what I've done:
Electronic Brat
function myFunction(){ alert("Coming!"); $(":text").blur(function() { var element=$(this); // you can get the element here }); $(":text").focusout(function() { alert(this.id + " focus out"); alert(("#this.id").val()); //alert((this).val()); }); }
Try writing your function out of jQuery(document).ready(function() {...})
jQuery(document).ready(function() {...});
function myFunction(){
$(":text").blur(function() {
alert("**");
var element=$(this); // you can get the element here
});
$(":text").focusout(function() {
alert(this.id + " focus out");
});
}
answered Apr 6, 2016 at 5:50
salin kunwar
1,1884 gold badges14 silver badges24 bronze badges
5 Comments
Electronic Brat
I tried this. That error is gone, but I'm not getting that alert in that onchange. Why is that so?
Electronic Brat
Thanks dude, for your help. I got it. But now, I face another problem. I'm trying to get the value of that text box, where I get "TypeError: this.id.val is not a function" error. How to resolve this problem and get the value. This is what I've done:
Electronic Brat
function myFunction(){ alert("Coming!"); $(":text").blur(function() { var element=$(this); // you can get the element here }); $(":text").focusout(function() { alert(this.id + " focus out"); alert(("#this.id").val()); //alert((this).val()); }); }
salin kunwar
You want to get the value of that textbox when blur?
salin kunwar
function myFunction(){ $("input[type=text]").blur(function() { var element=$(this); var value=element.val(); console.log(value); }); } see console tab to see its value
lang-js