1

I am facing a problem in Jquery ajax...

I want to pass a value in a function, this is my code:

$.each(data, function (index, value) {
    id = value['req_id'];
    notification.onclick = function () {
        alert(id);return false;

        $.ajax({
            url     : "<?php echo base_url(); ?>dashboard/notificationUpdated",
            type    : 'post',
            data    : {'id':id},
            dataType: "json",
            success : function(data){}
        });     
    };
});

when id is alerted it shows "undefined". How to solve this ,i want to pass this value to controller.... Can anyone help me with this??? Waiting for a response........

data =>

{  
   "0":{  
      "req_id":"368"
   },
   "1":{  
      "req_id":"371"
   },
   "2":{  
      "req_id":"372"
   },
   "3":{  
      "req_id":"373"
   },
   "4":{  
      "req_id":"375"
   },
   "5":{  
      "req_id":"376"
   },
   "6":{  
      "req_id":"378"
   },
   "7":{  
      "req_id":"379"
   },
   "8":{  
      "req_id":"380"
   },
   "9":{  
      "req_id":"382"
   },
   "10":{  
      "req_id":"385"
   },
   "11":{  
      "req_id":"386"
   },
   "12":{  
      "req_id":"387"
   },
   "13":{  
      "req_id":"399"
   },
   "14":{  
      "req_id":"400"
   },
   "15":{  
      "req_id":"404"
   },
   "16":{  
      "req_id":"405"
   },
   "17":{  
      "req_id":"406"
   },
   "18":{  
      "req_id":"418"
   },
   "19":{  
      "req_id":"419"
   },
   "20":{  
      "req_id":"422"
   },
   "21":{  
      "req_id":"424"
   },
   "22":{  
      "req_id":"425"
   },
   "23":{  
      "req_id":"428"
   },
   "24":{  
      "req_id":"429"
   },
   "25":{  
      "req_id":"430"
   },
   "26":{  
      "req_id":"431"
   },
   "27":{  
      "req_id":"432"
   },
   "28":{  
      "req_id":"434"
   },
   "status":"success"
}
2
  • 1
    put console.log(value) to show what it returned Commented Sep 7, 2016 at 5:47
  • outside onclick function id is printed ,but inside function it shows undefined. Commented Sep 7, 2016 at 5:52

3 Answers 3

2

Try this:

$.each(data, function (index, value) {
                   var id = value['req_id'];
                   notification.onclick = function () {
                     alert(id);

                      $.ajax({
                        url     : "<?php echo base_url(); ?>dashboard/notificationUpdated",
                        type    : 'post',
                        data    : { id: id },
                        dataType: "json",
                        success : function(data){}
                      });

                };
                });
Sign up to request clarification or add additional context in comments.

Comments

1

Use id as below

id=data[index].req_id;

pass id paramater into function

remove the return false from the code

3 Comments

$.each(data, function (index, value) { id = value['req_id']; notification.onclick (id); }); notification.onclick = function (id) { alert(id);return false; $.ajax({ url : "<?php echo base_url(); ?>dashboard/notificationUpdated", type : 'post', data : {'id':id}, dataType: "json", success : function(data){} }); };
declare function outside the loop and call it from loop
shows error " TypeError: notification.onclick is not a function "
1

Hello Try with below code it will be help you, here it display error of data variable but dont worry about it. because you have already declared and have value :)

$(document).ready(function(){
	$.each(data, function (index, value) {
		$(notification).click(function () {
			id = value['req_id'];
			alert(id);return false;
			$.ajax({
				url     : "<?php echo base_url(); ?>dashboard/notificationUpdated",
				type    : 'post',
				data    : {'id':id},
				dataType: "json",
				success : function(data){}
			});

		});
	});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

can you add data variable in your question description?

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.