0

I have the following code snippet that defines the property values in my form.

function retrieve(){
    setSelectedIndex(document.producerSetForm.GA1,"<%=ssnGA1%>");
    setSelectedIndex(document.producerSetForm.GA2,"<%=ssnGA2%>");
    setSelectedIndex(document.producerSetForm.GA3,"<%=ssnGA3%>");
    setSelectedIndex(document.producerSetForm.GA4,"<%=ssnGA4%>");
    setSelectedIndex(document.producerSetForm.GA5,"<%=ssnGA5%>");
}

where these ssnGA1,ssnGA2 etc may or may not be having a value. I need to check whether whether they have a value to do some more processing. I tried

var len=<%=ssnGA1.toString().length()%>;
if(len !=0)

but it works only if the value is present. else it giving javascript error. Please help. THANKS

user1002448
  • 425
  • 8
  • 22

1 Answers1

0

You have to check if your string is not undefined/null first, on example:

if ( ssnGA1 && ssnGA1.toString().length ) {
  // do something
}

Also, length is a property, not a function, see MDN for details.

kamituel
  • 34,606
  • 6
  • 81
  • 98
  • var str = <%=ssnGA2.toString()%>; alert(typeof(str)); I am getting the type as number. hence not able to check string against the length or any other property. – user1002448 Apr 22 '13 at 09:23
  • Try: var str = "<%=ssnGA2.toString()%>"; (I'm guessing ssnGA2.toString() returns a number). – kamituel Apr 22 '13 at 09:42
  • i tried that and its not working. since its returning number not able perform any check. returning java script error. str is having a number. if ssnGA2 property is not having a value var str = <%=ssnGA2.toString()%> won't work. need some alternative to check that – user1002448 Apr 22 '13 at 09:45