-2

I want to be able to access the value of a nested each() function outside of the function.

I have code below or http://jsfiddle.net/z36UK/6/

HTML:

<div id="wrap">
  <ul data-role="listview" data-filter="true" data-filter-placeholder="Search..." id="ds-canho"> 
  </ul>
</div>

Javascript:

$(document).ready(function() {
  var urlx=[];  
  parseXml();
  alert(urlx);
  console.log(urlx);

  $.mobile.loading( "show" );
  $.ajax({
    type: "GET",
    url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%27http://saigonpearl.info/home/notype/-0-trang-chu.html%27%20and%20xpath%3D%22%2F%2Fdiv%5B%40class%3D%27tindang%27%5D%22&diagnostics=true',
    dataType: "xml",
    success: parseXml
  });


  function parseXml(xml) {
    $(xml).find("div.tindang").each(function() {

      var url = $(this).find('a').attr("href"),
      urlx= url.replace('#menutop','');
      $("ul#ds-canho").append('<li>' + $(this).find('h3').text() + '</li>');
      $('ul#ds-canho').listview('refresh');
      $.mobile.loading( "hide" );
    });
  }

});
long.luc
  • 1,191
  • 1
  • 10
  • 30
user3709908
  • 89
  • 2
  • 8

2 Answers2

0

It's not about the each, is it? You can easily put the urlx variable there and assign to it from the inside:

function parseXml(xml) {
    var urlx;
    $(xml).find("div.tindang").each(function() {
        var url = $(this).find('a').attr("href"),
        urlx = url.replace('#menutop','');
        $("ul#ds-canho").append('<li>' + $(this).find('h3').text() + '</li>');
        $('ul#ds-canho').listview('refresh');
        $.mobile.loading( "hide" );
    });
    console.log(urlx);
    // if you wanted an array, use `urlx = [];` and `urlx.push(…);`
}

However, where you currently have placed the log and alert statements is

  • before the ajax request
  • outside of the ajax request callback

which means that it cannot work. You need to put them inside the callback, because you cannot return the data outside of it.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Please update below js code

$(document).ready(function() {                   
  $.mobile.loading( "show" );
  $.ajax({
    type: "GET",
    url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%27http://saigonpearl.info/home/notype/-0-trang-chu.html%27%20and%20xpath%3D%22%2F%2Fdiv%5B%40class%3D%27tindang%27%5D%22&diagnostics=true',
    dataType: "xml",
    success: parseXml
  });
  $( document ).ajaxComplete(function() {
    parseXml();
  });
  function parseXml(xml) {
    window.urlx = '';
    $(xml).find("div.tindang").each(function() {

      var url = $(this).find('a').attr("href"),
          urlx = url.replace('#menutop','');

      $("ul#ds-canho").append('<li>' + $(this).find('h3').text() + '</li>');
      $('ul#ds-canho').listview('refresh');
      $.mobile.loading( "hide" );
    });
  }
});

EDIT

Add FSFIDDLE

long.luc
  • 1,191
  • 1
  • 10
  • 30
Rohit Batham
  • 1,238
  • 1
  • 9
  • 13