-2

Am getting data from ajax call and appending the data to table. Am getting title and url from the data. Now when i click on the url data of table , i need to call a outer javascript function with the clicked url data.

 <script>

 jquery(document).ready(function(){
   jquery(#button).click(function{
  showData();  
 });
 });


function showData(){
var total  = "";
var final = ""; 
  jquery.ajax({
url : "AJAX_POST_URL",
type: "POST",
data : input,
    dataType: "json",
success: function(jsondata)
{
    //data - response from server
     var document= jsondata.data.results;  //am getting array of objects
     for(int i=0; i<document.length; i++){
      var tr = "<tr>";
    var td = "<td>" + "Title : " +
    document[i].title + "Url :" + "<a href=''>" +  document[i].url + </a> + "</td></tr>";
   final = tr + td;
    total= total + final ;
        }

    $("#docTable").append(total) ;

},
error: function (jqXHR, textStatus, errorThrown)
{
            alert("error");
}
});
}
</script>


<script>
function download(){

  //inside this function i need to get the value of document[i].url
}
</script> 
Afju
  • 57
  • 1
  • 3
  • 9
  • @adeneo : I have read the link..but I need to catch the data after clicking on data of the table and pass it to outer function.? how to do that? – Afju Dec 21 '13 at 19:17
  • could anybody help me in getting this...! – Afju Dec 21 '13 at 19:27

1 Answers1

0

You really should structure your program quite a bit better, and it would be easier if you could put your complete example up on jsfiddle.net.

But a quick solution in your current code may be to make "document" a global variable. Beware that document already is a global variable in the BOM (Browser Object Model), so use something else like say "docs".

<script>
// Global variable
var docs;

jquery(document).ready(function(){
  jquery(#button).click(function{
    showData();  
  });
});

function showData(){
  var total  = "";
  var final = "";

  jquery.ajax({
    url : "AJAX_POST_URL",
    type: "POST",
    data : input,
    dataType: "json",
    success: function(jsondata)
    {
      //data - response from server
      docs = jsondata.data.results;  //am getting array of objects

      for(int i=0; i<document.length; i++) {
        var tr = "<tr>";
        var td = "<td>" + "Title : " +
        docs[i].title + "Url :" + "<a href=''>" +  docs[i].url + </a> + "</td></tr>";
        final = tr + td;
        total = total + final;
      }

      $("#docTable").append(total) ;
    },

    error: function (jqXHR, textStatus, errorThrown)
    {
      alert("error");
    }
  });
}
</script>


<script>
function download(){    
  //inside this function i need to get the value of document[i].url

  // You have access to docs through the global variable
  for(var i=0;i < docs.length;i++) {
    console.log(docs[i].url)
  }
}
</script> 
Hallvar Helleseth
  • 1,867
  • 15
  • 17