Hello Everyone i'm writing a JS code which reads an XML file and extract required info from it, I want to find the name of a selected item, this is my code:

function initialize() {
    ct = $('menu').children().length;

    for(cati=0;cati<=ct-1;cati++) {
        cats[cati] = $('menu').children().eq(cati).prop('nodeName');
        var realname = cats[cati];
        if(realname.indexOf("0") != -1){
	       realname = realname.replace(/0/g,'/');
	realnames[cati] = realname;

    var selecteditem = $(".selected").text();

    for(j=0;j<=cats.length;j++) {
        if(selecteditem == realnames[j]) {
                selecteditem = cats[j];

here in my code when alerting using the last stmt there are two scenarios, the first one is when the if statement condition doesn't achieved, and in this case the alerting gives me the desired result(the selected item), but when the if statement condition achieved, the alerting gives me undifined although i'm certain that the cats[j] is an element in the XML file.