0

I have 2 variable 'aassn' and 'addd' in xml format. my concern is if 'Idc' of 'aassn' is blank then dont need 'Idc' value in output and same if 'Idd' of 'addd' is blank then dont need in output. If, 'Idc' and 'Idd' contaning data then need both in output. where aassn=

<?xml version="1.0" encoding="UTF-8"?>
<info>
 <name>
  <Idc>81</Idc>
 </name>
</info>

and addd=

my code is

var ssn1 = aassn.match(/<Idc\/>/);
var acc1 = addd.match(/<Idd\/>/);
var tempaassn = String(aassn);
var tempacc = String(addd);
var star1 = "Hi,\n\nDetails";

if (typeof(ssn1) != 'undefined' && ssn1 != '' && String(ssn1) != '' && tempaassn.indexOf('</Idc>') > -1) 
star1+= "\n\nBelow first data:\n\n" +  aassn.split("<Idc>")[1].split( "</Idc>" )[0];

if (typeof(acc1) != 'undefined' && acc1 != '' && String(acc1) != '' && tempacc.indexOf('</Idd>') > -1) 
star1+= "\n\nBelow secound data\n\n" +  addd.split("<Idd>")[1].split( "</Idd>" )[0];

return star1;

But i am gettitng output as below while Idd of addd is blank:

 Hi,
 Details:
 Below first data:
 81
 Below secound data:
NL1
  • 17
  • 6

2 Answers2

0

Not sure about your question, but you could check the length property of a string to check if it contains something:

var data = "";

if(data.length > 0){
    // Evaluates to true.
}
Koen
  • 422
  • 3
  • 16
0
if(typeof(myvariable) != 'undefined')
{
    // Variable is initialised i.e. data is set
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Jay Teli
  • 530
  • 1
  • 11
  • 19
  • I tried, but not working, var ssn1 = aassn.match(//); var acc1 = addd.match(//); var tempaassn = String(aassn); var tempacc = String(addd); var star1 = "Hi,\n\nDetails"; if (typeof(ssn1) != 'undefined' && ssn1 != '' && String(ssn1) != '' && tempaassn.indexOf('') > -1) star1+= "\n\nBelow first data:\n\n" + aassn.split("")[1].split( "" )[0]; if (typeof(acc1) != 'undefined' && acc1 != '' && String(acc1) != '' && tempacc.indexOf('') > -1) star1+= "\n\nBelow secound data\n\n" + addd.split("")[1].split( "" )[0]; return star1; – NL1 Oct 19 '16 at 14:09
  • check if this helps .. http://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized – Jay Teli Oct 19 '16 at 14:11
  • not working, i am gettitng output as below when Iddd of addd is blank: Hi, Details: Below first data: 81 Below secound data: , but i need Hi, Details: Below first data: 81 – NL1 Oct 19 '16 at 14:18
  • please check the values of each variable in the conditions. does it help? – Jay Teli Oct 19 '16 at 14:21