Ad

How To Use Variable In Find (Jquery)

- 1 answer

I have to confirm the parent node have particular node childs. I using the function to reduce code. but variable not work in find. help me to correct the below code

<x>
<ad>
<cd>
</cd>
</ad>
</x>

I have similar structure in xml code and i need to confirm x has child ad and ad has child cd.

var rd = new FileReader();
            rd.onload = function(e){  
                var xmlDoc = $.parseXML(this.result);                   
                var $xml = $(xmlDoc);
                checkparent('ad','cd');
                function checkparent(x,y)
                {                   
                    if($xml.find(x y).length > 0) //it shows error
                    {
                    alert("true");
                    }
                    else
                    {alert("false");}
                }
            };
            rd.readAsText(this.files[i]);
Ad

Answer

To fix this issue you have to modify your .find() call as follows:

if($xml.find(y + " > " + x).length > 0)
{ 
    alert("true"); 
} 
else {
    alert("false");
}
Ad
source: stackoverflow.com
Ad