0

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

2 Answers 2

1

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

2 Comments

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:
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()); }); }
1

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

5 Comments

I tried this. That error is gone, but I'm not getting that alert in that onchange. Why is that so?
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:
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()); }); }
You want to get the value of that textbox when blur?
function myFunction(){ $("input[type=text]").blur(function() { var element=$(this); var value=element.val(); console.log(value); }); } see console tab to see its value

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.